RELATED
I need to detect when an event is fired. To do so I am trying to subscribe dynamically on the event.
The problem is that I have different types of delegates, not all events have the same signature. The solutions provided in here and here expects object sender, EventArgs e, which I am not using, so I get an exception telling the types doesn’t match.
Here are some examples of a delegates I have:
public delegate void OnEventA(int id);
public delegate void OnEventB(double num, string name);
How can I create the correct delegate?
After some research I found some articles:
It helped me to understand what I was trying to do and I should do.
I need to use
Delegate.CreateDelegatepassing theEventHandlerType(the type of the event, the delegate), a instance of a class and the method info of the method (from the class in the previous parameter) that will handle the event. Target is the control that fires this event.Further digging lead me to this method. I can subscribe to events using lambda expression. Using
Action<T>I can subscribe with different types and numbers of parameters.Using this method (e is the EventInfo; EventManager is the class with the static method above)