CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2008
    Posts
    9

    Late Binding - Help invoking a method please.

    Hi,

    I am using reflection / late binding to invoke a method. This all works correctly as long as my paramater types match exactly the parameter types for the method.

    However I need it to attempt to parse the supplied parameters and convert their types to match that of the method being invoked. I guess its possible through setting some flags on the Binder ?? however I cannot get this to work.



    Here is my code so far (which will error if an incorrect paramater type is passed)


    object[] methodParams = { "x", "y" };

    Type thisType = this.GetType();

    MethodInfo methodToCall = thisType.GetMethod("MyMethod");

    methodToCall.Invoke(this, methodParams);


    Thanks,
    Martyn.

  2. #2
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Late Binding - Help invoking a method please.

    If you look at the MethodInfo class you'll see the method GetParameters which'll get you the types of all the parameters.

    You have to do the conversion yourself given the each parameter type and the type of the entry in the object [] array you pass to invoke.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  3. #3
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Late Binding - Help invoking a method please.

    I'm not sure this is possible to do it in runtime. How one could cast to runtime type? I'm affraid that mharris has only the option to check if the parameters match and if they don't, that he should throw exception or silently quit. But without at least basic knowleage about the types at compile time, I cannot imagine it.

    mharris, would it be possible to for the type to implement an interface and call the methoud trought this interface? I think this approach would make all much more easier.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  4. #4
    Join Date
    Jun 2008
    Posts
    9

    Re: Late Binding - Help invoking a method please.

    Quote Originally Posted by boudino
    mharris, would it be possible to for the type to implement an interface and call the methoud trought this interface? I think this approach would make all much more easier.
    Hi boudino,

    Thats not really an option. Im developing an ASP .NET server control. The idea is that methods on the server can be remotely called from client side javascript. So the method name and an array of parameters will be sent from the client, processed on the server and then returned all via XML HTTP.

    Im trying to build a generic solution that will form the underlying communications layer so specifically creating an interface is'nt really what im looking for.

    The obvious problem i face is that javascript isn't strongly typed. so i need a way of dealing with it if "10" is passed instead of 10 into a method that requires an integer, etc..


    Im still interested to know what the Binder does and if this can help. It has a method called ChangeType, and an enumeration for ExactctBinding which specifices that types should match up exactly

    Quote Originally Posted by MSDN
    ExactBinding: Specifies that types of the supplied arguments must exactly match the types of the corresponding formal parameters.

    The general principle is that ChangeType should perform only widening coercions, which never lose data.

    http://msdn.microsoft.com/en-us/libr...gs(VS.71).aspx

    anybody know much about this?

    Thanks again.

  5. #5
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Late Binding - Help invoking a method please.

    Hm... and what about make all parametrs of all server methods to be of object type? In other words, create a facade about the real methods. Than any type should fit, and in this very generic method, you can dispatch the target method, invoke it withou reflection and pass arguments of properly types to it.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  6. #6
    Join Date
    Jun 2008
    Posts
    9

    Re: Late Binding - Help invoking a method please.

    Quote Originally Posted by boudino
    Hm... and what about make all parametrs of all server methods to be of object type?

    I like the sound of that, it may just be the way forward. Then I can at least do some convertion or casting within the method itself.

    Thanks.

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Late Binding - Help invoking a method please.

    Actually I would go in the complete OTHER direction, and not have "callable methods".

    Instead, I would implement ONLY method which took a string (for the "Method Name" and an array of objects (for all of the parameters).

    Then you can use a dispatch table based on routed Dictionaries.

    I use this for a similar situation. I regularly embed a browser in my desktop UI's. All of the application interface is written in HTML / JavaScript (and was recently ported to silverlight).

    However to do "real" things, I need the Javascript from the pages to invoke functionallity in my desktop application.

    So exactly the same functionallity with a different topology....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  8. #8
    Join Date
    Jun 2008
    Posts
    9

    Re: Late Binding - Help invoking a method please.

    Quote Originally Posted by TheCPUWizard
    Instead, I would implement ONLY method which took a string (for the "Method Name" and an array of objects (for all of the parameters).

    Then you can use a dispatch table based on routed Dictionaries.

    Im not sure what you mean by dispatch table / routed dictionary. Is it possible you could explain or provide an example

    Thanks.

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

Featured