VBForums >
.NET >
C# > [RESOLVED] [2.0] Dynamic function call through Reflection
Click to See Complete Forum and Search --> : [RESOLVED] [2.0] Dynamic function call through Reflection
RudiVisser
Nov 13th, 2007, 04:17 PM
Hi!
I've been looking/reading around for this for most of the day, and have been unable to find answers suitable, only 'Reflection is what you need, read about it here xxxxxx'. So I have, but I don't understand it...
It basically says I should create an instance of the function then get the method type.. But but, I already know the type, they're all 'public bool FunctionName()' and I just want to call them...
Does anybody have some super duper simple code that will work??
Thanks :wave:
jmcilhinney
Nov 13th, 2007, 04:39 PM
You call GetType on your object to get a Type object. You call Type.GetMethod or GetMethods to get one or more MethodInfo objects. You call MethodInfo.Invoke to invoke that method on your object.
Atheist
Nov 13th, 2007, 04:42 PM
Heres a simple example:
Dim mInfo As System.Reflection.MethodInfo = TextBox1.GetType.GetMethod("AppendText")
mInfo.Invoke(TextBox1, New Object() {"Hello"})
Edit: Touché jmcilhinney, you beat me too it:D
Edit2: This was in C#? Aww I posted Vb.Net code, anyways its done the same in C# so..yeah;)
RudiVisser
Nov 13th, 2007, 05:54 PM
Okay you don't understand, either that or I don't... =/
What's with the .GetType bit??
Here's my code (Not at all exact cos I'm on the laptop.. Probably won't even work but you get the idea):
public bool iWantToCallThis(MIMClient client, string Message) {
// handle and return
return true;
}
public bool parseMessage(MIMClient client, string Message) {
// do processing on the message, from a switch get the name (string) of a function to call to handle that message appropriately
Dictionary<string, string> dict = new Dictionary<string, string>();
dict["TEST"] = "iWantToCallThis";
// Message == "TEST" in this example...
string funcName = dict[Message];
// So now that I have the name.. What next?
}
:afrog: :afrog:
EDIT: Okay so I have this -
using System.Reflection;
MethodInfo mInfo = ????????.GetType().GetMethod(funcName);
mInfo.Invoke(client, Message);
EDIT2: Oooooooooo I think I got it!!!!!!
The class goes before the .GetType, so:
MethodInfo mInfo = this.GetType().GetMethod(funcName);
mInfo.Invoke(client, Message);
Am I on the right lines with that?? Can't test it as like I say I'm on the laptop... :thumb:
jmcilhinney
Nov 13th, 2007, 06:05 PM
You call GetType on your object to get a Type object.If your object is the current instance then yes, you call GetType on 'this'.
RudiVisser
Nov 13th, 2007, 06:10 PM
It's just a function, in a class, being called from the same class as the function calling it, so in this case it would be 'this'?
Atheist
Nov 13th, 2007, 06:24 PM
It's just a function, in a class, being called from the same class as the function calling it, so in this case it would be 'this'?
The class;)
RudiVisser
Nov 13th, 2007, 06:26 PM
'this', then. You could just say yes :lol:
I got tired of waiting and let the programmer's urge take over (tried it). Here's the final (and working) code:
MethodInfo method = this.GetType().GetMethod(commands[Message]);
object[] param = new object[2] { client, Message };
method.Invoke(this, param);
Thanks for your help :)
*Updated* :thumb:
jmcilhinney
Nov 13th, 2007, 06:50 PM
We could just say "yes" but we would like to see you apply some logic to the situation. I post here not to solve people's problems but to try to make them better developers, and to do that I want to push people to think for themselves.
Do you know what 'this' actually is? This makes it appear not:It's just a function, in a class, being called from the same class as the function calling it, so in this case it would be 'this'?It's not just a function in a class. It is an instance function, so it gets called on instances of the class, not the class itself. When you use 'this' in code you are referring to the current instance of the current class. If the object on which you want to invoke the method is the current instance of the current class then, logically, you would use 'this', which exists solely to refer to the current instance of the current class.
That aside, this:I got tired of waiting and let the programmer's urge take over (tried it).is what you should have done in the first place. A good developer will test for themselves first and ask questions if it doesn't work.
RudiVisser
Nov 14th, 2007, 04:04 AM
It's not just a function in a class. It is an instance function, so it gets called on instances of the class, not the class itself. When you use 'this' in code you are referring to the current instance of the current class. If the object on which you want to invoke the method is the current instance of the current class then, logically, you would use 'this', which exists solely to refer to the current instance of the current class.
Yes I do understand that, but I didn't understand exactly what the Reflection functions needed, that's the point.
That aside, this is what you should have done in the first place. A good developer will test for themselves first and ask questions if it doesn't work.
As I said I was actually on my laptop and didn't have Visual Studio, nevermind a test-case for this to try it on...
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.