开发者

Monitoring non-dynamic method calls when working with child classes inheriting from DynamicObject

开发者 https://www.devze.com 2023-01-14 02:39 出处:网络
Let\'s say I have a class which inherits from DynamicObject: public class DynamicBase : DynamicObject {

Let's say I have a class which inherits from DynamicObject:

public class DynamicBase : DynamicObject
{
   public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
      //Yadda yadda yadda
    }

    //same for TrySetMember
}

and then I have a child class which inherits from DynamicBase:

public class ChildClass : DynamicBase
{
   public void SomeProperty { get; set; }
}

I'd like to monitor both the getter and setter of "SomeProperty" in the child class automatically. Currently, because SomeProperty exists in the child, methods are not routed to TryGet/SetMember or any other override of DynamicObject.

Ideally I would like this behavior after the user of the base class instantiates the object, like so:

var someInstance = new ChildClass();
someInstance.SomeProperty = "someValue" //Monitored somehow in DynamicBase

as opposed to having to create an instance of DynamicBase, with the child passed as a type (which is how Moq creates interceptors using DynamicProxy):

var someInstance = new Dyn开发者_运维技巧amicBase<ChildClass>();

I'm wondering if:

  1. This is even possible with C#?
  2. If DynamicObject is the wrong base class; should I drop down to IDynamicMetaObjectProvider or something else?
  3. If I need to take the route of DynamicBase to create a proxy around ChildClass with the combined behavior I'm looking for, using something like DynamicProxy to intercept calls?


This isn't possible with the dynamic infrastructure added in 4.0. Simply implementing IDynamicMetaObjectProvider doesn't mean your class gets to completely overrule .NET's type / member resolution. It just means that when an instance is referenced in the dynamic context, calls that can't be resolved otherwise statically will fall back to the dynamic provider.

0

精彩评论

暂无评论...
验证码 换一张
取 消