471,337 Members | 867 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Reflection and Dynamic Methods

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

Jun 27 '08 #1
10 1947
"Ron" <rs********@yahoo.comwrote:
m_ButtonControl.Click += new CommandEventHandler(methinf);
Error 1 'methinf' is a 'variable' but is used like a 'method'
I'm not sure what CommandEventHandler is, but this way works for me in
Windows Forms:

MethodInfo methinf = type.GetMethod("NameOfMethod");
theButton.Click += new EventHandler(MethodCall);

Eq.
Jun 27 '08 #2
Oh, ignore the previous post. I made a mistake.

Eq.
Jun 27 '08 #3
On Apr 16, 1:35*pm, "Paul E Collins" <find_my_real_addr...@CL4.org>
wrote:
"Ron" <rs_herh...@yahoo.comwrote:
m_ButtonControl.Click += new CommandEventHandler(methinf);
Error 1 'methinf' is a 'variable' but is used like a 'method'

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
Jun 27 '08 #4
On Apr 16, 12:58*pm, "Ron" <rs_herh...@yahoo.comwrote:
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);
Jun 27 '08 #5
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

"Ignacio Machin ( .NET/ C# MVP )" <ig************@gmail.comwrote in
message
news:f7**********************************@e67g2000 hsa.googlegroups.com...
On Apr 16, 12:58 pm, "Ron" <rs_herh...@yahoo.comwrote:
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);

Jun 27 '08 #6
On Apr 16, 2:14*pm, "Ron" <rs_herh...@yahoo.comwrote:
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
Jun 27 '08 #7
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
"Ignacio Machin ( .NET/ C# MVP )" <ig************@gmail.comwrote in
message
news:2e**********************************@26g2000h sk.googlegroups.com...
On Apr 16, 2:14 pm, "Ron" <rs_herh...@yahoo.comwrote:
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

Jun 27 '08 #8
On Apr 16, 3:39*pm, "Ron" <rs_herh...@yahoo.comwrote:
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)
Jun 27 '08 #9
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;
}
"Ignacio Machin ( .NET/ C# MVP )" <ig************@gmail.comwrote in
message
news:a0**********************************@l28g2000 prd.googlegroups.com...
On Apr 16, 3:39 pm, "Ron" <rs_herh...@yahoo.comwrote:
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)

Jun 27 '08 #10
On Apr 16, 4:47*pm, "Ron" <rs_herh...@yahoo.comwrote:
Ignacio,

Im Still getting the problem... here is my code:
What error are you getting now?
It should be different than the first ime
Jun 27 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Derek Hart | last post: by
1 post views Thread by Derek Hart | last post: by
1 post views Thread by Matthias Klöpper | last post: by
12 posts views Thread by Antony | last post: by
2 posts views Thread by Stefan | last post: by
2 posts views Thread by Luis Arvayo | last post: by
7 posts views Thread by BK | last post: by
8 posts views Thread by =?Utf-8?B?U2hhd24=?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.