473,722 Members | 2,430 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.GetExe cutingAssembly( );
string currentNamespac e =
Assembly.GetExe cutingAssembly( ).GetTypes()[0].Namespace;
Type type = assem.GetType(c urrentNamespace + "." + m_FormName, true, true);
MethodInfo methinf = type.GetMethod( MethodCall);
m_ButtonControl .Click += new CommandEventHan dler(methinf);

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

Thanks,
Ron

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

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

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_a ddr...@CL4.org>
wrote:
"Ron" <rs_herh...@yah oo.comwrote:
m_ButtonControl .Click += new CommandEventHan dler(methinf);
Error 1 'methinf' is a 'variable' but is used like a 'method'

I'm not sure what CommandEventHan dler 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...@yah oo.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.GetExe cutingAssembly( );
string currentNamespac e =
Assembly.GetExe cutingAssembly( ).GetTypes()[0].Namespace;
Type type = assem.GetType(c urrentNamespace + "." + *m_FormName, true, true);
MethodInfo methinf = type.GetMethod( MethodCall);
m_ButtonControl .Click += new CommandEventHan dler(methinf);

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

Thanks,
Ron
Hi,

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

//Hook the event.
System.Reflecti on.EventInfo evClick =
c.GetType().Get Event(eventName );
Type tDelegate = evClick.EventHa ndlerType;
Delegate d = Delegate.Create Delegate(tDeleg ate,
Field.ContentPr ovider, targetMethodNam e);
System.Reflecti on.MethodInfo addHandler =
evClick.GetAddM ethod();
Object[] addHandlerArgs = { d };
addHandler.Invo ke(c, addHandlerArgs) ;
Jun 27 '08 #5
Ron
Ignacio,

Good Stuff!

One question: What is Field.ContentPr ovider?

I'm getting an ArgumentExcepti on : Error Binding to target method on this
line:
Delegate d = Delegate.Create Delegate(tDeleg ate, methinf);

Thanks!
Ron

"Ignacio Machin ( .NET/ C# MVP )" <ig************ @gmail.comwrote in
message
news:f7******** *************** ***********@e67 g2000hsa.google groups.com...
On Apr 16, 12:58 pm, "Ron" <rs_herh...@yah oo.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.GetExe cutingAssembly( );
string currentNamespac e =
Assembly.GetExe cutingAssembly( ).GetTypes()[0].Namespace;
Type type = assem.GetType(c urrentNamespace + "." + m_FormName, true,
true);
MethodInfo methinf = type.GetMethod( MethodCall);
m_ButtonControl .Click += new CommandEventHan dler(methinf);

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

Thanks,
Ron
Hi,

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

//Hook the event.
System.Reflecti on.EventInfo evClick =
c.GetType().Get Event(eventName );
Type tDelegate = evClick.EventHa ndlerType;
Delegate d = Delegate.Create Delegate(tDeleg ate,
Field.ContentPr ovider, targetMethodNam e);
System.Reflecti on.MethodInfo addHandler =
evClick.GetAddM ethod();
Object[] addHandlerArgs = { d };
addHandler.Invo ke(c, addHandlerArgs) ;

Jun 27 '08 #6
On Apr 16, 2:14*pm, "Ron" <rs_herh...@yah oo.comwrote:
Ignacio,

Good Stuff!

One question: What is Field.ContentPr ovider?

Sorry, that is part of my code :) , take a look at
Delegate.Create Delegate 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.Create Delegate(tDeleg ate,frmTest,met hinf);

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******** *************** ***********@26g 2000hsk.googleg roups.com...
On Apr 16, 2:14 pm, "Ron" <rs_herh...@yah oo.comwrote:
Ignacio,

Good Stuff!

One question: What is Field.ContentPr ovider?

Sorry, that is part of my code :) , take a look at
Delegate.Create Delegate 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...@yah oo.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.Create Delegate(tDeleg ate,frmTest,met hinf);
It's easy , my code use this overload:
Delegate.Create Delegate (Type, Object, String)

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

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.R ibbon.ButtonCom mand CreateButton()
{
m_ButtonControl = new Janus.Windows.R ibbon.ButtonCom mand();
m_ButtonControl .Text = m_ButtonText;
if (m_ImageName.Tr im().Length 0)
{
m_ButtonControl .LargeImage =
(Image)rm.GetOb ject(m_ImageNam e);
}
Assembly assem = Assembly.GetExe cutingAssembly( );
string currentNamespac e =
Assembly.GetExe cutingAssembly( ).GetTypes()[0].Namespace;
Type type = assem.GetType(c urrentNamespace + "." + m_FormName,
true, true);
MethodInfo methinf = type.GetMethod( MethodCall);

System.Reflecti on.EventInfo evClick =
m_ButtonControl .GetType().GetE vent("Click");
Type tDelegate = evClick.EventHa ndlerType;
Delegate d = Delegate.Create Delegate(tDeleg ate, type,
MethodCall);
System.Reflecti on.MethodInfo addHandler =
evClick.GetAddM ethod();
Object[] addHandlerArgs = { d };
//addHandler.Invo ke(c, addHandlerArgs) ;

return m_ButtonControl ;
}
"Ignacio Machin ( .NET/ C# MVP )" <ig************ @gmail.comwrote in
message
news:a0******** *************** ***********@l28 g2000prd.google groups.com...
On Apr 16, 3:39 pm, "Ron" <rs_herh...@yah oo.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.Create Delegate(tDeleg ate,frmTest,met hinf);
It's easy , my code use this overload:
Delegate.Create Delegate (Type, Object, String)

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

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3696
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 discuss how reflection does this, but I cannot find the syntax to do this. I have tried several code example off of gotdotnet and other articles. Can somebody please show me the code to do this?
1
2257
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 it will run the method in the current class, but I need to read parameters from a database into the params object, and this is where I am stuck. It seems that the params may need to be actual variables, but I am not sure. How can I take a list of...
1
3039
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 just fine and when invoked the functions are executed perfectly well. The only problem is that I seem to be unable to get back any return values. To simplify the whole case I created a test-setup that binds to user32.dll, dynamically creates a...
12
2369
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? Thank you for you help. Tony Imports System.Reflection Public Class Form1
2
2678
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 dynamic and scalable business objects. http://www.developersdex.com/gurus/articles/739.asp Thanks for your feedback, it's much appreciated.
9
1604
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, classes, & properties declared in the xml file? Thanks
2
3638
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
2227
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 business classes dynamically, so my natural inclination was to use reflection. The problem I'm running into is that my business classes require arguments to be passed in where as the forms did not. Here is an example of launching a form:
8
3861
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 i would like to be able to do is start with and empty class, then depending on the data provided to me by a config file, add the member variables and attributes to the class temporarily. when the app is shutdown all changes would be gone. can...
0
9384
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9157
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5995
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.