Let’s say I have this:
dynamic foo = new Foobar();
And I have this:
public class Foobar : DynamicObject
{
}
The question is, is it possible to override members of DynamicObject so that this code:
string name = new Foobar().Name
Does not throw an Exception at run-time? I want to return default for name‘s if Name is not a member.
Possible? What do I need to override?
Override TryGetMember (and TrySetMember). Classes derived from the DynamicObject class can override this method to specify dynamic behavior for operations such as getting a value for a property.
http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.aspx
http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.trygetmember.aspx