Reflection and Dynamic Methods

R

Ron

I have a situation where I have an object name as a string, and a method as
a string. I need to construct a click handler with those two bits of
information. This is what I came up with so far... and the error message I
am receiving...

Assembly assem = Assembly.GetExecutingAssembly();
string currentNamespace =
Assembly.GetExecutingAssembly().GetTypes()[0].Namespace;
Type type = assem.GetType(currentNamespace + "." + m_FormName, true, true);
MethodInfo methinf = type.GetMethod(MethodCall);
m_ButtonControl.Click += new CommandEventHandler(methinf);

Error 1 'methinf' is a 'variable' but is used like a 'method'

Thanks,
Ron
 
I

Ignacio Machin ( .NET/ C# MVP )

I'm not sure what CommandEventHandler is, but this way works for me in
Windows Forms:

It's a delegate, to be used in the onCommand event in web controls
 
I

Ignacio Machin ( .NET/ C# MVP )

I have a situation where I have an object name as a string, and a method as
a string.  I need to construct a click handler with those two bits of
information.  This is what I came up with so far... and the error message I
am receiving...

Assembly assem = Assembly.GetExecutingAssembly();
string currentNamespace =
Assembly.GetExecutingAssembly().GetTypes()[0].Namespace;
Type type = assem.GetType(currentNamespace + "." +  m_FormName, true, true);
MethodInfo methinf = type.GetMethod(MethodCall);
m_ButtonControl.Click += new CommandEventHandler(methinf);

Error 1 'methinf' is a 'variable' but is used like a 'method'

Thanks,
Ron

Hi,

Take a look at this code:
string targetMethodName = "yourmethod";
string eventName = "eventName";

//Hook the event.
System.Reflection.EventInfo evClick =
c.GetType().GetEvent(eventName);
Type tDelegate = evClick.EventHandlerType;
Delegate d = Delegate.CreateDelegate(tDelegate,
Field.ContentProvider, targetMethodName);
System.Reflection.MethodInfo addHandler =
evClick.GetAddMethod();
Object[] addHandlerArgs = { d };
addHandler.Invoke(c, addHandlerArgs);
 
R

Ron

Ignacio,

Good Stuff!

One question: What is Field.ContentProvider?

I'm getting an ArgumentException : Error Binding to target method on this
line:
Delegate d = Delegate.CreateDelegate(tDelegate, methinf);

Thanks!
Ron

message
I have a situation where I have an object name as a string, and a method
as
a string. I need to construct a click handler with those two bits of
information. This is what I came up with so far... and the error message I
am receiving...

Assembly assem = Assembly.GetExecutingAssembly();
string currentNamespace =
Assembly.GetExecutingAssembly().GetTypes()[0].Namespace;
Type type = assem.GetType(currentNamespace + "." + m_FormName, true,
true);
MethodInfo methinf = type.GetMethod(MethodCall);
m_ButtonControl.Click += new CommandEventHandler(methinf);

Error 1 'methinf' is a 'variable' but is used like a 'method'

Thanks,
Ron

Hi,

Take a look at this code:
string targetMethodName = "yourmethod";
string eventName = "eventName";

//Hook the event.
System.Reflection.EventInfo evClick =
c.GetType().GetEvent(eventName);
Type tDelegate = evClick.EventHandlerType;
Delegate d = Delegate.CreateDelegate(tDelegate,
Field.ContentProvider, targetMethodName);
System.Reflection.MethodInfo addHandler =
evClick.GetAddMethod();
Object[] addHandlerArgs = { d };
addHandler.Invoke(c, addHandlerArgs);
 
I

Ignacio Machin ( .NET/ C# MVP )

Ignacio,

Good Stuff!

One question: What is Field.ContentProvider?


Sorry, that is part of my code :) , take a look at
Delegate.CreateDelegate method to know the parameter expected there,
and replace it with your value, IIRC it's the instance object that
will be hooked to tha event
 
R

Ron

I checked out the documentation and its even less clear :)

The Form that the method is in is called frmTest.

I tried Delegate d = Delegate.CreateDelegate(tDelegate,frmTest,methinf);

and I get an Error binding to target method on that line.

Ron


message
Ignacio,

Good Stuff!

One question: What is Field.ContentProvider?


Sorry, that is part of my code :) , take a look at
Delegate.CreateDelegate method to know the parameter expected there,
and replace it with your value, IIRC it's the instance object that
will be hooked to tha event
 
I

Ignacio Machin ( .NET/ C# MVP )

I checked out the documentation and its even less clear :)

The Form that the method is in is called frmTest.

I tried  Delegate d = Delegate.CreateDelegate(tDelegate,frmTest,methinf);

It's easy , my code use this overload:
Delegate.CreateDelegate (Type, Object, String)

As you see, the first parameter is a Type instance, that I created in
the line above
Type tDelegate = evClick.EventHandlerType;

The second parameter is the instance of the class that will handle the
event ( in your case would be an isntance of the frmTest)
and finally the last parameter is the name of the method (its a member
of frmTest)
 
R

Ron

Ignacio,

Im Still getting the problem... here is my code:

public Janus.Windows.Ribbon.ButtonCommand CreateButton()
{
m_ButtonControl = new Janus.Windows.Ribbon.ButtonCommand();
m_ButtonControl.Text = m_ButtonText;
if (m_ImageName.Trim().Length > 0)
{
m_ButtonControl.LargeImage =
(Image)rm.GetObject(m_ImageName);
}
Assembly assem = Assembly.GetExecutingAssembly();
string currentNamespace =
Assembly.GetExecutingAssembly().GetTypes()[0].Namespace;
Type type = assem.GetType(currentNamespace + "." + m_FormName,
true, true);


MethodInfo methinf = type.GetMethod(MethodCall);

System.Reflection.EventInfo evClick =
m_ButtonControl.GetType().GetEvent("Click");
Type tDelegate = evClick.EventHandlerType;
Delegate d = Delegate.CreateDelegate(tDelegate, type,
MethodCall);
System.Reflection.MethodInfo addHandler =
evClick.GetAddMethod();
Object[] addHandlerArgs = { d };
//addHandler.Invoke(c, addHandlerArgs);

return m_ButtonControl;
}


message
I checked out the documentation and its even less clear :)

The Form that the method is in is called frmTest.

I tried Delegate d = Delegate.CreateDelegate(tDelegate,frmTest,methinf);

It's easy , my code use this overload:
Delegate.CreateDelegate (Type, Object, String)

As you see, the first parameter is a Type instance, that I created in
the line above
Type tDelegate = evClick.EventHandlerType;

The second parameter is the instance of the class that will handle the
event ( in your case would be an isntance of the frmTest)
and finally the last parameter is the name of the method (its a member
of frmTest)
 
I

Ignacio Machin ( .NET/ C# MVP )

Ignacio,

Im Still getting the problem... here is my code:

What error are you getting now?
It should be different than the first ime
 

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