CreateDelegate in VS.NET 2003 - error on binding method


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?
 
Ad

Advertisements

S

snibril

Sorry, posted wrong error message, here is the message I get:

"An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll
Additional information: Error binding to target method."

Can anyone help?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top