using System; using System.Collections.Generic; namespace SUISS.Core { public class Messenger { 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)] = (Action)Delegate.Combine(this.events[typeof(T)], action); } else { this.events.Add(typeof(T), action); } this.lookupTable.Add(callback, action); } public void Unsubscribe(Action callback) where T : class { if (this.events.ContainsKey(typeof(T)) && this.lookupTable.ContainsKey(callback)) { (this.events)[typeof(T)] = (Action)Delegate.Remove(this.events[typeof(T)], this.lookupTable[callback]); this.lookupTable.Remove(callback); } } public void Invoke(T evt) where T : class { Action action; if (this.events.TryGetValue(typeof(T), out action) && action != null) { action(evt); } } private Dictionary> events = new Dictionary>(); private Dictionary> lookupTable = new Dictionary>(); } }