获取在动态对象中调用方法的通用类型 [英] Get generic type of call to method in dynamic object

查看:160
本文介绍了获取在动态对象中调用方法的通用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用.Net中的动态对象,我无法弄清楚如何做某事。



我有一个继承自DynamicObject的类,我覆盖TryInvokeMember方法。



例如

  class MyCustomDynamicClass :DynamicObject 
{
public override bool TryInvokeMember(InvokeMemberBinder binder,object [] args,out object result)
{
//我想知道这里的泛型参数的类型
}
}

在该方法里面我想知道类型如果有的话)调用中的通用参数。



eg
如果我调用以下代码,我想在我的动态对象的覆盖方法中获取System.Boolean和System.Int32的值

  dynamic myObject = new MyCustomDynamicClass(); 
myObject.SomeMethod< bool>(arg);
myObject.SomeOtherMethod< int>(arg);

目前,如果我在重写方法中放置一个断点,我可以获取被调用的方法的名称SomeMethod和SomeOtherMethod,以及参数的值,而不是通用类型)。



如何获取这些值?



谢谢!

解决方案

实际上,我查看了binder的层次结构,找到了一个属性在对象的内部字段中具有所需的值。



问题是该属性不会因为使用C#特定的代码/类而被公开,因此属性必须使用Reflection访问。



我在这个日本博客中找到了代码: http://neue.cc/category/programming (我不看日文,所以我不知道作者是否真的描述了这个相同的问题。



这是代码片段:

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

typeArgs是一个包含调用方法时使用的泛型参数类型的列表。



希望这有助于别人。


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 TryInvokeMember method.

e.g.

class MyCustomDynamicClass : DynamicObject
{
    public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
    {
        // I want to know here the type of the generic argument
    }
}

And inside that method I want to know the type (if any) of the generic arguments in the invocation.

e.g. If I invoke the following code, I want to get the value of System.Boolean and System.Int32 inside the overrided method of my dynamic object

dynamic myObject = new MyCustomDynamicClass();
myObject.SomeMethod<bool>("arg");
myObject.SomeOtherMethod<int>("arg");

Currently if I place a breakpoint inside the overrided method I can get the name of the method being invoked ("SomeMethod" and "SomeOtherMethod", and also the values of the arguments, but not the generic types).

How can I get these values?

Thanks!

解决方案

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<Type>);

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

Hope this helps someone else.

这篇关于获取在动态对象中调用方法的通用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆