如果绑定器名称为 GetType() 或 ToString() 等,则 TryInvokeMember 不会触发
我只是在摆弄 C# 4.0 动态关键字,并对一件事感到好奇。
假设我有一个类 DynamicWeirdness : DynamicObject
在其中我有一个名为 reference
的字段,它也是 dynamic
类型。还有一个名为 referencetype
的字段,其类型为 Type
这是我的构造函数:
public DynamicWeirdness(object reference)
{
this.reference = reference;
this.referencetype = reference.GetType();
}
如果我尝试这样做:
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
if (binder.Name == "GetType" && args.Length == 0)
{
result = referencetype;
return true;
}
result = null;
return false;
}
当我调用 a 的 GetType()
时DynamicWeirdness
对象,它只是忽略我的调用并返回 {Name = "DynamicWeirdness" FullName = "Dynamic1.DynamicWeirdness"}
。为什么?
我尝试过使用 ToString()
、GetHashCode()
,并且发生了同样的事情。
I'm just messing around with the C# 4.0 dynamic keyword, and got curious about one thing.
Suppose I have a class DynamicWeirdness : DynamicObject
Inside it I have a field named reference
which is also of type dynamic
. And a field named referencetype
which is of type Type
This is my constructor:
public DynamicWeirdness(object reference)
{
this.reference = reference;
this.referencetype = reference.GetType();
}
If I ever try this:
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
if (binder.Name == "GetType" && args.Length == 0)
{
result = referencetype;
return true;
}
result = null;
return false;
}
When I call GetType()
of a DynamicWeirdness
object, it simply ignores my invocation and returns {Name = "DynamicWeirdness" FullName = "Dynamic1.DynamicWeirdness"}
. Why?
I've tried with ToString()
, GetHashCode()
, and the same thing happens.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 DynamicObject 文档:
由于 DynamicObject 继承自 Object,因此 Object 的任何方法都会阻止 TryInvokeMember 处理调用。
According to the documentation for DynamicObject:
Since DynamicObject inherits from Object, any methods of Object will preclude TryInvokeMember from processing the call.
方法
GetType()
、ToString()
和GetHashCode()
均在DynamicObject
上定义(因为它继承自System.Object
)。当 .NET 调用这些方法时,它只会直接调用它们,因为它们是在对象上定义的,并且会跳过对TryInvokeMember
的调用。如果您尝试调用不同的方法(例如
Substring()
),您可以看到此操作的实际效果,并且您会看到DynamicWeirdness
上的TryInvokeMember
会执行此操作接到电话。您只需在
DynamicWeirdness
上创建一个新的GetType()
方法,而不是重写DynamicWeirdness
上的TryInvokeMember
以返回不同的类型。代码>.对于
GetHashCode()
和ToString()
,您可以覆盖DynamicWeirdness
上的这些成员,因为它们被标记为虚拟。The methods
GetType()
,ToString()
, andGetHashCode()
are all defined onDynamicObject
(since it inherits fromSystem.Object
). When .NET goes to invoke those methods, it will just call them directly since they are defined on the object, and will skip the call toTryInvokeMember
.You can see this in action if you try calling a different method, like
Substring()
, and you'll see thatTryInvokeMember
onDynamicWeirdness
does get called.Instead of overriding
TryInvokeMember
onDynamicWeirdness
to return a different type, you can just create a newGetType()
method onDynamicWeirdness
.For
GetHashCode()
andToString()
, you can override those members onDynamicWeirdness
since they are marked as virtual.