Using generics for strongly typed event handlers?
c#, generics, parameters, templates, types
Solution
You can just pass the generic type to the argument.. like this:
class Receiver {
public RegisterEvent<T>(T param);
}
The `T` in the argument list must match the type provided into the call.
EDIT: Having re-read the question.. perhaps this is actually what you meant:
public delegate void CustomEventHandler<T>(T param);
public void RegisterEvent<T>(CustomEventHandler<T> eh) {
}
..same philosophy though.
Problem
I am trying to implement something along the lines of ``` class Receiver { public RegisterEvent<T>(???); } class EventTypeClass { ... } class MyApp { public MyApp() { RegisterEvent<EventTypeClass>(MyEventHandler); } void MyEventHandler(EventTypeClass param) { // Handle event of type 'EventTypeClass' } } ``` I am not sure how I can pass a strongly typed event handler as a parameter or if it is possible at all, or am I forced to use something like ``` void MyEventHandler(object param) { var castedParam = param as EventTypeClass; } ```