Click to See Complete Forum and Search --> : Late Binding - Help invoking a method please.
mharris
June 19th, 2008, 12:57 PM
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.
darwen
June 19th, 2008, 04:21 PM
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.
boudino
June 20th, 2008, 01:33 AM
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.
mharris
June 20th, 2008, 05:52 AM
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
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/library/system.reflection.bindingflags(VS.71).aspx
anybody know much about this?
Thanks again.
boudino
June 20th, 2008, 06:08 AM
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.
mharris
June 20th, 2008, 08:56 AM
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.
TheCPUWizard
June 21st, 2008, 12:04 AM
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....
mharris
June 23rd, 2008, 06:41 AM
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.