using System; using System.Collections.Generic; public class EventDispatcher { public void Subscribe(Action callback) where T : class { Action action = delegate(object o) { callback((T)((object)o)); }; if (!this._events.ContainsKey(typeof(T))) { this._events[typeof(T)] = new HashSet>(); } this._events[typeof(T)].Add(action); this._lookupTable.Add(callback, action); } public void Unsubscribe(Action callback) where T : class { if (this._events.ContainsKey(typeof(T))) { this._events[typeof(T)].Remove(this._lookupTable[callback]); } this._lookupTable.Remove(callback); } public void Invoke(T evt) where T : class { HashSet> hashSet; if (this._events.TryGetValue(typeof(T), out hashSet) && hashSet != null) { foreach (Action action in hashSet) { action(evt); } } } private Dictionary>> _events = new Dictionary>>(); private Dictionary> _lookupTable = new Dictionary>(); }