I’m sure I saw a video of Anders Heijlsberg doing something like this but anyway, I knocked this up in about five minutes and it’s well smart!
This is an example of what you can do in C#4.0 with dynamic:
What’s cool is that bag has no such property at compile time called Test or Stuff. And even if they did, “Stuff” couldn’t be of the type that gets assigned to it as that type is generated anonymously.
So how do you do this? I was looking through wikipedia on C#4.0, having seen a nice video on Channel 9 on the language, and saw the following sentence:
Dynamic lookup is performed using three distinct mechanisms: COM IDispatch for COM objects,
IDynamicObject DLR interface for objects implementing that interface, and Reflection for all other objects. Any C# class can therefore intercept dynamic calls on its instances by implementing
So, I opened up VS2010, created a class and tried to inherit from DynamicObject. At first I got nowhere, and couldn’t even find the Microsoft.CSharp assembly to reference, until I realised that for some reason VS2010 had started my project targeting .NET 3.5 instead of 4.0. Having fixed this, I was able to quickly knock up my dynamic property bag class:
So what happens when we try to access a property on dynamic? Well, my (hugely limited understanding) is that the C# runtime binder first sees if it can resolve the property request itself. If it can’t (as in our case), it then asks the DLR to see if it can do it. So the DLR sees if the object implements DynamicObject, and if it does, it then calls one of the above methods TrySetMember and TryGetMember.
And it just works! I’m still not entirely sure of the times when we should and shouldn’t use this. But it’s quite cool to see nonetheless 🙂