Last week I learned something new and very powerful about the dynamic keyword in C#. I already was aware of the possibility of creating your own dynamic object by deriving from the DynamicObject class. Here is a short example.
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";
You don't even need to create your own MyDynamicObject, but you can use System.Dynamic.ExpandoObject. It even implements IDictionary<string, object> so you can foreach over its members. More information about the ExpandoObject
The above example with ExpandoObject:
dynamic exp = new ExpandoObject();
exp.FirstName = "Boaz";
This works because the compiler converts the dynamic type to classes in Microsoft.CSharp.RuntimeBinder and System.Runtime.CompilerServices. These classes will invoke the DLR at runtime.
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.