【问题标题】:create a dictionary to map enums to events创建字典以将枚举映射到事件
【发布时间】:2012-09-30 05:16:51
【问题描述】:

我正在尝试创建一个字典来将枚举映射到具有相同签名的一组事件。我写道:

public enum Events {Insert, Update, Delete};

// this part makes errors
Dictionary<Events,EventHandler<T>> EventsDic = new Dictionary<Events,EventHandler<T>>()
{
     { Events.Insert , this.ItemInserted}
};

怎么了?

【问题讨论】:

    标签: asp.net events dictionary delegates


    【解决方案1】:

    问题是T 必须替换为类型,因为您要声明一个变量。 T 必须是字典中 value 的类型,或者在您的特定情况下,是事件参数的类型。既然您特别说您要存储事件,T 可能应该是旧的EventArgs。您的代码应如下所示:

    Dictionary<Events,EventHandler<EventArgs>> EventsDic = new Dictionary<Events,EventHandler<EventArgs>>()
    {
         { Events.Insert , this.ItemInserted}
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      相关资源
      最近更新 更多