开发者

Implementing dynamic capabilities on an object without inheriting DynamicObject?

Right now I have a class that extends DynamicOb开发者_JAVA技巧ject and overrides TryGetMember.

public class FieldCollection : DynamicObject, ICollection<Field>, ISerializable
{
    ...

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        var field = _inner.TryGetField(binder.Name);

        result = field == null ? null : field.Value;
        return true;
    }

    ...
}

dynamic fields = new FieldCollection();
Console.WriteLine(fields.Foo);

This works fine, but I'm forced to extend DynamicObject which means I can't extend anything else. Is it possible to do do this without extending DynamicObject?


You can implement IDynamicMetaObjectProvider yourself. It's a lot more work.


You can delegate to a child DynamicObject, like this answer (which starts from @Lee's answer, but with additonal work done) to a duplicate SO question.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜