Get generic type of call to method in dynamic object

后端 未结 3 867
感动是毒
感动是毒 2021-02-07 08:49

I\'m starting to work with dynamic objects in .Net and I can\'t figure out how to do something.

I have a class that inherits from DynamicObject, and I override the TryIn

3条回答
  •  执念已碎
    2021-02-07 09:53

    Actually I looked through the hierarchy of the binder and found a property with the needed values in the internal fields of the object.

    The problem is that the property isn't exposed because it uses C#-specific code/classes, therefore the properties must be accessed using Reflection.

    I found the code in this japanese blog: http://neue.cc/category/programming (I don't read any japanese, therefore I'm not sure if the author actually describes this same issue

    Here's the snippet:

    var csharpBinder = binder.GetType().GetInterface("Microsoft.CSharp.RuntimeBinder.ICSharpInvokeOrInvokeMemberBinder");
    var typeArgs = (csharpBinder.GetProperty("TypeArguments").GetValue(binder, null) as IList);
    

    typeArgs is a list containing the types of the generic arguments used when invoking the method.

    Hope this helps someone else.

提交回复
热议问题