|
-
Nov 13th, 2007, 05:17 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [2.0] Dynamic function call through Reflection
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
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Nov 13th, 2007, 05:39 PM
#2
Re: [2.0] Dynamic function call through Reflection
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.
-
Nov 13th, 2007, 05:42 PM
#3
Re: [2.0] Dynamic function call through Reflection
Heres a simple example:
VB.Net Code:
Dim mInfo As System.Reflection.MethodInfo = TextBox1.GetType.GetMethod("AppendText")
mInfo.Invoke(TextBox1, New Object() {"Hello"})
Edit: Touché jmcilhinney, you beat me too it
Edit2: This was in C#? Aww I posted Vb.Net code, anyways its done the same in C# so..yeah
-
Nov 13th, 2007, 06:54 PM
#4
Thread Starter
Hyperactive Member
Re: [2.0] Dynamic function call through Reflection
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):
Code:
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?
}

EDIT: Okay so I have this -
Code:
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:
Code:
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...
Last edited by RudiVisser; Nov 13th, 2007 at 07:00 PM.
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Nov 13th, 2007, 07:05 PM
#5
Re: [2.0] Dynamic function call through Reflection
 Originally Posted by jmcilhinney
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'.
-
Nov 13th, 2007, 07:10 PM
#6
Thread Starter
Hyperactive Member
Re: [2.0] Dynamic function call through Reflection
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'?
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Nov 13th, 2007, 07:24 PM
#7
Re: [2.0] Dynamic function call through Reflection
 Originally Posted by RudiVisser
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
-
Nov 13th, 2007, 07:26 PM
#8
Thread Starter
Hyperactive Member
Re: [2.0] Dynamic function call through Reflection
Last edited by RudiVisser; Nov 13th, 2007 at 07:41 PM.
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Nov 13th, 2007, 07:50 PM
#9
Re: [RESOLVED] [2.0] Dynamic function call through Reflection
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.
-
Nov 14th, 2007, 05:04 AM
#10
Thread Starter
Hyperactive Member
Re: [RESOLVED] [2.0] Dynamic function call through Reflection
 Originally Posted by jmcilhinney
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.
 Originally Posted by jmcilhinney
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...
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|