views:

335

answers:

1

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?

+3  A: 

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

Dino Viehland