473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 2118
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
1
by: Derek Hart | last post by:
I have the following code: Me.GetType().InvokeMember(FunctionToRun, BindingFlags.InvokeMethod, Nothing, Me, params) I can send in a string from a database into the FunctionToRun variable, and...
1
by: Matthias Klöpper | last post by:
Hi there, I'm currently trying to call some API-Functions via Reflection.Emit since I need to bind to different dlls based on user input. The dynamic creation of the required PInvoke-Methods works...
12
by: Antony | last post by:
Hello - I am wanting to print out a "Yes" next to classes that implement "Interface01", otherwise a "No". Here is my code. It crashes with a null reference exception and I am not sure why. Ideas?...
2
by: Stefan | last post by:
Hello, I was hoping my fellow coders would give me some feedback on the article I wrote which makes use of the System.Reflection namespace and Polymorphism to demonstrate how you can create...
9
by: TS | last post by:
i have code that creates assemblies and classes from the assemlby and methods from the classes to set properties of dynamically created controls. How i should go about validating the assemblies,...
2
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
7
by: BK | last post by:
I've used reflection quite a bit to launch forms, it works great, requires little coding, and allows me to launch forms from a dynamic menu. Now I have a need to instantiate any one of several...
8
by: =?Utf-8?B?U2hhd24=?= | last post by:
Hi; i just started research reflection and i'm wondering if i have an empty class file can i use reflection to add member variables and attributes dynamically and then instantiate the class? What...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.