static public void Unregister <T>(OnEventFunction <T> oe) where T : Event { Type type = typeof(T); if (eventHandles.ContainsKey(type)) { List <Func <Event, MulticastDelegate> > list = eventHandles[type]; for (int i = 0; i < list.Count; i++) { if (oe == (OnEventFunction <T>)list[i](null)) { list.RemoveAt(i); if (cleanUnusedLists && list.Count == 0) { eventHandles.Remove(type); } //Log.Debug("unregistered: " + oe.ToString()); return; } } } throw new Exception("nothing registered!"); }
public void Dispatch <T>(OnEventFunction <T> func) where T : Event { if (!Event.Handled && Exact ? TypeOfEvent == typeof(T) : Event is T) { func?.Invoke((T)Event); } }
static private Func <Event, MulticastDelegate> WrapType <T>(OnEventFunction <T> oef) where T : Event { return((e) => { if (e != null) { oef((T)e); return null; } else { return oef; } }); // bestest // https://stackoverflow.com/questions/32512153/dynamic-actiont-invalid-arguments-when-executed }
static public void Register <T>(OnEventFunction <T> oe) where T : Event { Type type = typeof(T); Func <Event, MulticastDelegate> func = WrapType(oe); if (eventHandles.ContainsKey(type)) { eventHandles[type].Add(func); } else { eventHandles.Add(type, new List <Func <Event, MulticastDelegate> > { func }); } //Log.Debug("registered: " + oe.ToString()); }