IDynamicObject 实现忽略多个属性调用

发布于 2024-07-09 03:49:43 字数 371 浏览 4 评论 0原文

我已经在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

镜花水月 2024-07-16 03:49:43

在这种情况下,无论您从 (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());

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文