参考网上博客以及Github上面的源码,最后的代码如下:
public interface IEventBus
{
void Suscribe(Type tevent, Action action);
void Suscribe<T1>(Type tevent, Action<T1> action);
void Suscribe<T1, T2>(Type tevent, Action<T1, T2> action);
void Suscribe<T1, T2, T3>(Type tevent, Action<T1, T2, T3> action);
void Suscribe<T1, T2, T3, T4>(Type tevent, Action<T1, T2, T3, T4> action);
void Suscribe<T1, T2, T3, T4, T5>(Type tevent, Action<T1, T2, T3, T4, T5> action);
void Suscribe<T1, T2, T3, T4, T5, T6>(Type tevent, Action<T1, T2, T3, T4, T5, T6> action);
void PostMessage(Type TEvent,params object[] args);
void UnSuscribe(Type tevent);
}
public class EventBus :IEventBus
{
private Dictionary<Type, List<EventArgs>> dict = new Dictionary<Type, List<EventArgs>>();
private static EventBus eventBus;
public static EventBus MyEventBus
{
get
{
if (eventBus == null)
{
eventBus = new EventBus();
}
return eventBus;
}
}
public void PostMessage(Type type,params object[] args)
{
List<EventArgs> arr = dict[type];
foreach(EventArgs eventArgs in arr)
{
eventArgs.ActiveMethod.Invoke(eventArgs.EventHost, args);
}
}
public void Suscribe(Type tevent,Action action)
{
if (!dict.Keys.Contains(tevent))
{
dict[tevent] = new List<EventArgs>();
}
dict[tevent].Add(new EventArgs()
{
EventHost = action.Target,
ActiveMethod = action.Method
});
}
public void Suscribe<T1>(Type tevent, Action<T1> action)
{
if (!dict.Keys.Contains(tevent))
{
dict[tevent] = new List<EventArgs>();
}
dict[tevent].Add(new EventArgs()
{
EventHost = action.Target,
ActiveMethod = action.Method
});
}
public void Suscribe<T1, T2>(Type tevent, Action<T1, T2> action)
{
if (!dict.Keys.Contains(tevent))
{
dict[tevent] = new List<EventArgs>();
}
dict[tevent].Add(new EventArgs()
{
EventHost = action.Target,
ActiveMethod = action.Method
});
}
public void Suscribe<T1, T2, T3>(Type tevent, Action<T1,T2,T3> action)
{
if (!dict.Keys.Contains(tevent))
{
dict[tevent] = new List<EventArgs>();
}
dict[tevent].Add(new EventArgs()
{
EventHost = action.Target,
ActiveMethod = action.Method
});
}
public void Suscribe<T1, T2, T3,T4>(Type tevent, Action<T1, T2, T3, T4> action)
{
if (!dict.Keys.Contains(tevent))
{
dict[tevent] = new List<EventArgs>();
}
dict[tevent].Add(new EventArgs()
{
EventHost = action.Target,
ActiveMethod = action.Method
});
}
public void Suscribe<T1, T2, T3,T4,T5>(Type tevent, Action<T1, T2, T3, T4, T5> action)
{
if (!dict.Keys.Contains(tevent))
{
dict[tevent] = new List<EventArgs>();
}
dict[tevent].Add(new EventArgs()
{
EventHost = action.Target,
ActiveMethod = action.Method
});
}
public void Suscribe<T1, T2, T3, T4, T5,T6>(Type tevent, Action<T1, T2, T3, T4, T5, T6> action)
{
if (!dict.Keys.Contains(tevent))
{
dict[tevent] = new List<EventArgs>();
}
dict[tevent].Add(new EventArgs()
{
EventHost = action.Target,
ActiveMethod = action.Method
});
}
public void UnSuscribe(Type tevent)
{
List<EventArgs> arr = dict[tevent];
arr.Clear();
arr = null;
}
}
public class EventArgs
{
public object EventHost { get; set; }
public MethodInfo ActiveMethod { get; set; }
}