The opinions expressed herein are my own personal opinions and do not represent
my employer's view in any way.
class MyDynamicObject : DynamicObject
Dictionary<string, object> _Values = new Dictionary<string, object>();
public override bool TryGetMember(GetMemberBinder binder, out object result)
result = _Values[binder.Name];
result = null;
public override bool TrySetMember(SetMemberBinder binder, object value)
_Values[binder.Name] = value;
Now you can create instances of MyDynamicObject and just set any member variable you want and access those as well, however you should be careful or runtime exceptions will occur.
dynamic test = new MyDynamicObject();
test.FirstName = "Boaz";
dynamic exp = new ExpandoObject();
exp.FirstName = "Boaz";
But what I didn't know is that you can't only do this for classes that inherit from DynamicObject, but for any object. A very simple example:
static void Main(string args)
static dynamic Add(dynamic a, dynamic b)
return a + b;
This is also known as Duck Typing, what means you identify an object by its members rather then by its Type or Interfaces it implements. Just like you most likely will identify a bird that walks, swims and quacks like a duck as a duck.
use the dynamic type the compiler will not check your properties, methods or
even operators, but will leave it for the DLR to process it. This can cause
very nasty runtime exceptions, so be careful just like you would when you would
typecast an object.
View the full example on gist.