Results 1 to 10 of 10

Thread: [RESOLVED] [2.0] Dynamic function call through Reflection

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Resolved [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2.0] Dynamic function call through Reflection

    Heres a simple example:
    VB.Net Code:
    1. Dim mInfo As System.Reflection.MethodInfo = TextBox1.GetType.GetMethod("AppendText")
    2.         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
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Dynamic function call through Reflection

    Quote 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'.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    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.

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2.0] Dynamic function call through Reflection

    Quote 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
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: [2.0] Dynamic function call through Reflection

    'this', then. You could just say yes

    I got tired of waiting and let the programmer's urge take over (tried it). Here's the final (and working) code:

    Code:
    MethodInfo method = this.GetType().GetMethod(commands[Message]);
    object[] param = new object[2] { client, Message };
    method.Invoke(this, param);
    Thanks for your help

    *Updated*
    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.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Ubuntu Haters Club
    Posts
    405

    Re: [RESOLVED] [2.0] Dynamic function call through Reflection

    Quote 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.

    Quote 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
  •  



Click Here to Expand Forum to Full Width