R
redhotsly
Hi,
Is is possible to create an event handler method that can handle any
type of event no matter what the delegate signature is.
Here is the code I have so far:
public class class1
{
public Delegate Hook(Type eventHandlerType)
{
Delegate eh = Delegate.CreateDelegate(
eventHandlerType,
this,
"Callback",
true );
return eh;
}
private void CallBack(object sender, EventArgs e)
{
Console.WriteLine("callback");
}
}
With the code above I can do:
Delegate d = _class1.Hook( typeof( EventHandler ) );
d.Method.Invoke(...);
But I cannot do it with other type of delegates, such as
CancelEventHandler:
Delegate d = _class1.Hook( typeof( CancelEventHandler ) );
d.Method.Invoke(...);
This fails at runtime. The call to Delegate.CreateDelegate fails with
an argument exception. Error: "Error binding to target method."
Is there a way to this work? After all CancelEventArg derives from
EventArg.
Thanks for your help.
Sylvain
Is is possible to create an event handler method that can handle any
type of event no matter what the delegate signature is.
Here is the code I have so far:
public class class1
{
public Delegate Hook(Type eventHandlerType)
{
Delegate eh = Delegate.CreateDelegate(
eventHandlerType,
this,
"Callback",
true );
return eh;
}
private void CallBack(object sender, EventArgs e)
{
Console.WriteLine("callback");
}
}
With the code above I can do:
Delegate d = _class1.Hook( typeof( EventHandler ) );
d.Method.Invoke(...);
But I cannot do it with other type of delegates, such as
CancelEventHandler:
Delegate d = _class1.Hook( typeof( CancelEventHandler ) );
d.Method.Invoke(...);
This fails at runtime. The call to Delegate.CreateDelegate fails with
an argument exception. Error: "Error binding to target method."
Is there a way to this work? After all CancelEventArg derives from
EventArg.
Thanks for your help.
Sylvain