private void RemoveTinyEvent <T>(ref ArrayList list, T handler) { TinyEvent <T> tinyEvent = null; for (int i = 0; i < list.Count; i++) { tinyEvent = list[i] as TinyEvent <T>; if (tinyEvent != null) { tinyEvent.UnRegisterEventHandler(handler); //本应该清除事件参数队列相应的EventParams,但是这个参数是持有对TinyEvent的引用,通过SendEvent去处理的,在上面已经remove了TinyEvent的handler,所以不用处理貌似也没毛病 } } }
/// <summary> /// 看看eventType对应的事件处理List是否有T类型的TinyEvent,有就调用注册事件, 没有先加一个再注册 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="list"></param> /// <param name="handler"></param> /// <returns></returns> private bool AddTinyEvent <T>(ref ArrayList list, T handler) { TinyEvent <T> tinyEvent = null; bool isExist = false; for (int i = 0; i < list.Count; i++) { tinyEvent = list[i] as TinyEvent <T>; if (tinyEvent != null) { isExist = true; } } if (!isExist) { //处理不存在事件的情况 tinyEvent = new TinyEvent <T>(); list.Add(tinyEvent); } return(tinyEvent.RegisterEventHandler(handler)); }
/// <summary> /// 异步发事件. /// </summary> /// <typeparam name="T1"></typeparam> /// <typeparam name="T2"></typeparam> /// <typeparam name="T3"></typeparam> /// <typeparam name="T4"></typeparam> /// <param name="eventType"></param> /// <param name="arg1"></param> /// <param name="arg2"></param> /// <param name="arg3"></param> /// <param name="arg4"></param> public void SendEvnetAsync <T1, T2, T3, T4>(EventType eventType, T1 arg1, T2 arg2, T3 arg3, T4 arg4) { ArrayList list = null; TinyEvent <Action <T1, T2, T3, T4> > tinyEvt = null; if (eventMapDic.ContainsKey(eventType)) { list = eventMapDic[eventType]; if (list != null) { for (int i = 0; i < list.Count; i++) { tinyEvt = list[i] as TinyEvent <Action <T1, T2, T3, T4> >; if (tinyEvt != null) { eventParamsQue.Enqueue(new EventParams <T1, T2, T3, T4>(tinyEvt, eventType, arg1, arg2, arg3, arg4));//这种new操作可以考虑用对象池分配,但是类型太多处理还要注意下 } } } } }
/// <summary> /// 异步发事件.现在的实现感觉不是很好,看以后有没有更好的方式 /// </summary> /// <param name="eventType"></param> public void SendEvnetAsync(EventType eventType) { ArrayList list = null; TinyEvent <Action> tinyEvt = null; if (eventMapDic.ContainsKey(eventType)) { list = eventMapDic[eventType]; if (list != null) { for (int i = 0; i < list.Count; i++) { tinyEvt = list[i] as TinyEvent <Action>; if (tinyEvt != null) { eventParamsQue.Enqueue(new EventParams(tinyEvt, eventType));//这种new操作可以考虑用对象池分配,但是类型太多处理还要注意下 } } } } }
/// <summary> /// 同步发事件. /// </summary> /// <typeparam name="T1"></typeparam> /// <typeparam name="T2"></typeparam> /// <typeparam name="T3"></typeparam> /// <typeparam name="T4"></typeparam> /// <param name="eventType"></param> /// <param name="arg1"></param> /// <param name="arg2"></param> /// <param name="arg3"></param> /// <param name="agr4"></param> public void SendEvnetSync <T1, T2, T3, T4>(EventType eventType, T1 arg1, T2 arg2, T3 arg3, T4 agr4) { ArrayList list = null; TinyEvent <Action <T1, T2, T3, T4> > tinyEvt = null; if (eventMapDic.ContainsKey(eventType)) { list = eventMapDic[eventType]; if (list != null) { for (int i = 0; i < list.Count; i++) { tinyEvt = list[i] as TinyEvent <Action <T1, T2, T3, T4> >; if (tinyEvt != null) { tinyEvt.SendEvent(arg1, arg2, arg3, agr4); } } } } }
/// <summary> /// 同步发事件. /// </summary> /// <param name="eventType"></param> public void SendEvnetSync(EventType eventType) { ArrayList list = null; TinyEvent <Action> tinyEvt = null; if (eventMapDic.ContainsKey(eventType)) { list = eventMapDic[eventType]; if (list != null) { for (int i = 0; i < list.Count; i++) { tinyEvt = list[i] as TinyEvent <Action>; if (tinyEvt != null) { tinyEvt.SendEvent(); } } } } }
public EventParams(TinyEvent <Action> _tinyEvt, EventType _evtType) { evtType = _evtType; tinyEvt = _tinyEvt; }