如何编写属于自己的事件集合

public class MyEvent
{
/// <summary>
/// 定义某一类事件的名称
/// </summary>
private const string EventName = "MyEvent";
/// <summary>
/// 定义某一类事件的名称
/// </summary>
private const string EventName1 = "MyEvent1";
/// <summary>
/// 事件集合
/// </summary>
private readonly Dictionary<string, List<object>> EventDic = new Dictionary<string, List<object>>();

/// <summary>
/// 定义事件代理句柄
/// </summary>
/// <param name="obj"></param>
public delegate void MyHandler(object obj);
/// <summary>
/// 定义事件代理句柄
/// </summary>
/// <param name="obj"></param>
public delegate void MyHandler1(string obj);

public event MyHandler MyEvents
{
add
{
EventDic[EventName].Add(value);
}
remove
{
EventDic[EventName].Remove(value);
}
}
public event MyHandler1 MyEvents1
{
add
{
EventDic[EventName1].Add(value);
}
remove
{
EventDic[EventName1].Remove(value);
}
}

public MyEvent()
{
EventDic.Add(EventName, new List<object>());//添加一类事件集合
EventDic.Add(EventName1, new List<object>());//添加一类事件集合
}

public void Run()
{
if (EventDic[EventName].Count == 0)
return;
foreach (object obj in EventDic[EventName])
{
(obj as MyHandler)(1);
}
}
public void Run1()
{
if (EventDic[EventName1].Count == 0)
return;
foreach (object obj in EventDic[EventName1])
{
(obj as MyHandler1)("");
}
}

}

 

你可能感兴趣的:(集合)