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.
精彩评论