获取动态对象中方法调用的通用类型
我开始使用 .Net 中的动态对象,但我不知道如何做某事。
我有一个继承自 DynamicObject 的类,并且我重写了 TryInvokeMember 方法。
例如
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
}
}
,在该方法中,我想知道调用中通用参数的类型(如果有)。
例如 如果我调用以下代码,我想在动态对象的重写方法内获取 System.Boolean 和 System.Int32 的值
dynamic myObject = new MyCustomDynamicClass();
myObject.SomeMethod<bool>("arg");
myObject.SomeOtherMethod<int>("arg");
当前,如果我在重写方法内放置断点,我可以获得正在调用的方法的名称( “SomeMethod”和“SomeOtherMethod”,以及参数的值,但不是泛型类型)。
我怎样才能得到这些值?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,我查看了绑定器的层次结构,并在对象的内部字段中找到了具有所需值的属性。
问题是该属性未公开,因为它使用特定于 C# 的代码/类,因此必须使用反射来访问该属性。
我在这个日本博客中找到了代码: http://neue.cc/category/programming (我不知道不懂日语,因此我不确定作者是否确实描述了同样的问题,
这是片段:
typeArgs 是一个列表,其中包含调用该方法时使用的通用参数的类型。
希望这对其他人有帮助。
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:
typeArgs is a list containing the types of the generic arguments used when invoking the method.
Hope this helps someone else.
谷歌搜索了一下,我对 .NET 和 Mono 有了相当通用的解决方案:
玩得开心。顺便说一句,Impromptu 很酷,但我无法使用它。
A bit of googling and I have quite generic solution for .NET and Mono:
Have fun. By the way Impromptu is cool, but I can't use it.
开源框架 Dynamitey 可以使用 DLR 调用内部/受保护/私有的属性,从而与 Silverlight 配合使用。但对于接口显式成员来说,这有点棘手,因为您必须使用类型上成员的实际全名,而不是接口成员名称。所以你可以这样做:
The open source framework Dynamitey can call properties that internal/protected/private using the DLR and thus works with Silverlight. But it get's a little tricky with interface explicit members as you have to use the actual full name of the member on the the type, rather than the interface member name. So you can do: