S
snibril
Hi,
I'm trying to delegate all events of a command button to a single
method, handlesAll. I wrote the following code in VS.NET 2005, and it
worked fine:
public void setUpDelegates()
{
MethodInfo mi = typeof(MainForm).GetMethod("handlesAll",
BindingFlags.Public | BindingFlags.Instance);
foreach (System.Reflection.EventInfo h in
typeof(Button).GetEvents())
{
h.AddEventHandler(button1,
System.Delegate.CreateDelegate(h.EventHandlerType, this, mi));
}
}
public void handlesAll(object sender, EventArgs e)
{
Console.WriteLine("Received event: " +
e.ToString());
}
As I said, this works fine in VS.NET 2005. However, in VS.NET 2003,
the overloaded version of CreateDelegate(Type, object, MethodInfo)
doesn't exist - I need to use CreateDelegate(Type, object, string), so
I tried doing
h.AddEventHandler(button1,
System.Delegate.CreateDelegate(h.EventHandlerType, this, mi.Name));
inside the loop. However, it now gives me the error message:
"An unhandled exception of type 'System.NullReferenceException'
occurred in TestGridEx.exe
Additional information: Object reference not set to an instance of an
object."
I've tried directly calling mi.Invoke() and it finds the method fine -
what am I doing wrong?
I'm trying to delegate all events of a command button to a single
method, handlesAll. I wrote the following code in VS.NET 2005, and it
worked fine:
public void setUpDelegates()
{
MethodInfo mi = typeof(MainForm).GetMethod("handlesAll",
BindingFlags.Public | BindingFlags.Instance);
foreach (System.Reflection.EventInfo h in
typeof(Button).GetEvents())
{
h.AddEventHandler(button1,
System.Delegate.CreateDelegate(h.EventHandlerType, this, mi));
}
}
public void handlesAll(object sender, EventArgs e)
{
Console.WriteLine("Received event: " +
e.ToString());
}
As I said, this works fine in VS.NET 2005. However, in VS.NET 2003,
the overloaded version of CreateDelegate(Type, object, MethodInfo)
doesn't exist - I need to use CreateDelegate(Type, object, string), so
I tried doing
h.AddEventHandler(button1,
System.Delegate.CreateDelegate(h.EventHandlerType, this, mi.Name));
inside the loop. However, it now gives me the error message:
"An unhandled exception of type 'System.NullReferenceException'
occurred in TestGridEx.exe
Additional information: Object reference not set to an instance of an
object."
I've tried directly calling mi.Invoke() and it finds the method fine -
what am I doing wrong?