473,320 Members | 2,133 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,320 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 2116
"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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.