Any help would be appreciated on this one. I’m trying to implement a dynamic object wrapper over a static type. This wrapper should then allow me to call static functions dynamically at run time.
For example:
dynamic obj = new StaticDynamicWrapper(typeof(MyStaticType));
obj.DoSomething(arg1);
That’s the idea. I got some online reference to get it to work on non-generic method and property but ran into various issues when “DoSomething” is actually a generic method.
For example, if the declaration of the “DoSomething” were as follow:
public static RetType DoSomething<TArg>(this TArg arg1);
Or even worse if do something were to have an overload with the following signature…
public static RetType DoSomething<TArg>(this OtherGenericType<AnotherGenericType<TArg>> arg1);
So I need to implement the DynamicObject.TryInvokeMember in such a way that the method invokation is able to infer at runtime, based on the runtime arguments (ie. object[] args), the correct closed generic method of DoSomething. In another word I wanna be able to select the correct overload and determines the correct type arguments to call MakeGenericMethod with, all that at runtime.
The biggest roadblock so far, is figuring out how to map the method’s open generic arguments to the closed types arguments declared by the parameter arguments (ie. object[] args). Can anyone out there help me out?
Thanks for your time!
The DLR will infer method overloads including generics. The open source framework ImpromptuInterface available via nuget simplifies the dlr calls to single method calls and does all the work for you. Infact it has a base class ImpromptuForwarder that will do this with a small constructor change.