public void AddListener <TEvent>(ApplicationEventHandlerDelegate <TEvent> handler) where TEvent : IApplicationEvent { Delegate @delegate; if (_applicationEventHandlers.TryGetValue(typeof(TEvent), out @delegate)) { _applicationEventHandlers[typeof(TEvent)] = Delegate.Combine(@delegate, handler); } else { _applicationEventHandlers[typeof(TEvent)] = handler; } }
public void RemoveListener <TEvent>(ApplicationEventHandlerDelegate <TEvent> handler) where TEvent : IApplicationEvent { Delegate @delegate; if (_applicationEventHandlers.TryGetValue(typeof(TEvent), out @delegate)) { Delegate currentDel = Delegate.Remove(@delegate, handler); if (currentDel == null) { _applicationEventHandlers.Remove(typeof(TEvent)); } else { _applicationEventHandlers[typeof(TEvent)] = currentDel; } } }
public void Dispatch <TEvent>(TEvent @event) where TEvent : IApplicationEvent { if (@event == null) { throw new ArgumentNullException("event"); } if (_disposed) { throw new ObjectDisposedException("Cannot dispatch and event when disposed! "); } Delegate @delegate; if (_applicationEventHandlers.TryGetValue(typeof(TEvent), out @delegate)) { ApplicationEventHandlerDelegate <TEvent> callback = @delegate as ApplicationEventHandlerDelegate <TEvent>; if (callback != null) { callback(@event); } } }