Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I dynamically call a method on a dynamic object?

When I want to dynamically call a statically-defined ("statically" in the sense of "determined at compile-time", not in the sense of "class-level member") method on any object in C#, I can use reflection to get a handle to that method and invoke it:

typeof(Foo).GetMethod("Bar").Invoke(foo, new object[] { /* params */ });

However, objects made dynamic by inheriting from DynamicObject respond to (undefined) instance method calls using TryInvokeMember, and the dynamic methods the class responds to are not exposed through reflection, for obvious reasons. This means that I can't get a method handle for a method that should be responded to by TryInvokeMember.

So ironically, it seems to me that you can't dynamically call a dynamic method on a dynamic object as easily as you can call a defined method on a non-dynamic object.

I've considered calling TryInvokeMember directly, but the first argument must be an instance of an InvokeMemberBinder, an abstract class. I feel that if I have to implement a class to call a dynamic method on a dynamic object, I must be doing something wrong.

How can I call a method on a dynamic object by its name, knowing that the target class does not implement it and that it should be responded to using TryInvokeMember?

like image 449
zneak Avatar asked Dec 05 '12 00:12

zneak


People also ask

Which operator is used to call the methods of dynamic objects?

A dynamic object is created using a "new" operator that returns a pointer to the newly constructed object and is destructed by a "delete" operator. A pointer variable is used to hold the pointer to the object that is returned by the "new" operator.

How do you call a method dynamically in Ruby?

Fortunately, Ruby's metaprogramming feature allows us to call methods dynamically by just passing the method name into public_send(method_name) or send(method_name) . We can call the make_noise method by calling Duck. new. public_send("make_noise") , this is equivalent to calling Duck.

How do you make an object dynamic?

You can create custom dynamic objects by using the classes in the System. Dynamic namespace. For example, you can create an ExpandoObject and specify the members of that object at run time. You can also create your own type that inherits the DynamicObject class.


2 Answers

I have an open source (Apache license) framework Dynamitey (available in nuget) which encapsulates the dynamic binder code, this includes automatically caching the call sites. It has convenience methods for every type of binder too (getters,setters, events, indexers, operators, conversions), but specifically you want InvokeMember.

The dynamic binder code actually runs faster than reflection (amortized) when calling statically defined (at compile time) members of classes too.

Dynamic.InvokeMember(foo,"Bar",arg...);
like image 157
jbtule Avatar answered Oct 17 '22 20:10

jbtule


One way to go about it is to mimic what the C# compiler outputs for method invocations on dynamic objects. This requires the usage of a bunch of types marked [EditorBrowsable(EditorBrowsableState.Never)] in the Microsoft.CSharp.RuntimeBinder namespace, so they will not be visible in Intellisense. Needless to say, this doesn't seem like a supported scenario, so use it at your own risk!

This code calls the dynamic Bar method without any arguments on an instance of a class derived from DynamicObject:

dynamic dynamicObject = new DerivedFromDynamicObject();
var callSiteBinder = Binder.InvokeMember(CSharpBinderFlags.None, "Bar", Enumerable.Empty<Type>(), typeof(Program),
    new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) });
var callSite = CallSite<Action<CallSite, object>>.Create(callSiteBinder);
callSite.Target(callSite, dynamicObject);

This blog post and this one have more gory details on call sites and binders.

like image 31
Trillian Avatar answered Oct 17 '22 21:10

Trillian