IDynamicObject 实现忽略多个属性调用
我已经在 C# 4 中实现了 IDynamicObject,返回一个自定义 MetaObject 子类,该子类执行简单的属性 getter/setter 分派到字典。 不是火箭科学。
如果我这样做:
dynamic foo = new DynamicFoo();
foo.Name = "Joe";
foo.Name = "Fred";
Console.WriteLine(foo.Name);
那么“Joe”就会打印到控制台...对“Name”设置器的第二次调用永远不会被调用(根本不会进入我的自定义调度程序代码)。
我知道 DLR 会进行调用站点缓存,但我认为这不适用于此处。 有人知道发生了什么事吗?
I've implemented IDynamicObject in C# 4, return a custom MetaObject subclass that does simple property getter/setter dispatch to a Dictionary. Not rocket science.
If I do this:
dynamic foo = new DynamicFoo();
foo.Name = "Joe";
foo.Name = "Fred";
Console.WriteLine(foo.Name);
Then 'Joe' is printed to the console... the second call to the 'Name' setter is never invoked (never steps into my custom dispatcher code at all).
I know the DLR does callsite caching, but I assumed that wouldn't apply here. Anyone know what's going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,无论您从 (Bind)SetMember 返回什么元对象都将被缓存并重新使用。 您有 2 个动态站点在做组数。 第一个调用会将结果缓存在二级缓存中,第二个站点将在要求您生成新规则之前获取该结果。
因此,无论您返回什么元对象,都需要包含一个将更新值的表达式树。 例如,它应该执行以下操作:
return new MetaObject(
Expression.AssignProperty(this.Expression, value.Expression),
Restrictions.TypeRestriction(this.Expression, this.Value.GetType());
Whatever MetaObject you're returning from (Bind)SetMember will be cached and re-used in this case. You have 2 dynamic sites doing sets. The 1st call will cache the result in an L2 cache which the 2nd site will pick up before asking you to produce a new rule.
So whatever MetaObject you're returning needs to include an expression tree that will update the value. For example it should do something like:
return new MetaObject(
Expression.AssignProperty(this.Expression, value.Expression),
Restrictions.TypeRestriction(this.Expression, this.Value.GetType());