在大一点的项目里时间分发器是很必要的,我直接上干货代码
//事件分发基类
using System;
using System.Collections.Generic;
namespace Logic.Base
{
public class MsgDispatcher<expand>where expand :class,new()
{
public delegate void Handler(expand param);
private Dictionary<int, List<Handler>> _handlers = new Dictionary<int, List<Handler>>();
public void Dispatch<CmdT>(CmdT cmd, expand mb = null)
{
List<Handler> list;
int key = Convert.ToInt32(cmd);
this._handlers.TryGetValue(key, out list);
if (list != null)
{
for (int i = 0; i < list.Count; i++)
{
list[i](mb);
}
}
}
public void Register<CmdT>(CmdT cmd, Handler handler)
{