Allrameest Allrameest -3 years ago 217
C# Question

Implementing dynamic capabilities on an object without inheriting DynamicObject?

Right now I have a class that extends DynamicObject 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?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download