Example #1
0
    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;
        }
    }
Example #2
0
    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;
            }
        }
    }
Example #3
0
    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);
            }
        }
    }