Created
September 25, 2014 14:25
-
-
Save rofr/06cc041ad235ec82ff7c to your computer and use it in GitHub Desktop.
Why? Co/contravariance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Register or reregister a generic handler | |
/// </summary> | |
public void Subscribe(Action<IEvent> eventHandler, Func<IEvent, bool> selector = null) | |
{ | |
_eventHandlers[new DelegateHandler(eventHandler)] = selector != null | |
? new DelegateSelector(selector) | |
: DelegateSelector.Any; | |
} | |
/// <summary> | |
/// Register or reregister a handler for all events of type T | |
/// </summary> | |
public void Subscribe<T>(Action<T> eventHandler) where T : IEvent | |
{ | |
//call to overload above fails: cant cast Action<T> to Action<IEvent> | |
Subscribe(eventHandler, e => e is T); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment