I talked a little bit about the new “dynamic” objects coming in C# 4.0, basically in the context of COM Interop, but here’s a refresher.
you can now go
Which is pretty neat in itself, as it’ll support Intellisense and all that fun stuff. To add to this, I just read about “ExpandoObjects”, which is a new class in new the “Dynamic Language Runtime”. Basically, an ExpandoObject is an easy way to create an object with whatever properties you need on the fly. Let’s do an example in the context of an XML node.
1 2 3 4 5 6 7 8 9 10 11
1 2 3 4 5 6 7 8
As you can see, we’ve instantiated an ExpandoObject (but as a dynamic object), and are basically declaring and setting properties on it as we need to. Also, to declare our sub-nodes (i.e. Address), we do the same thing and just set it as a property on the parent node. Not only is this a little cleaner to code, but all the properties you declare carry forward with the object. So now instead of this:
We can do this:
So that’s all fine and dandy, and works great for certain implementations (i.e. Data Transport Objects like the example). But what happens when you want a little more control over what happens within the ExpandoObject? That’s where DynamicObject comes in. DynamicObject is the base class that ExpandoObject extends, and allows you to override methods that are called whenever someone uses the object. By overriding methods like TryGetMember, TrySetMember, TryInvoke, etc., you can control exactly what happens within the dynamic object. Here’s a quick example of overriding the TryGetMember method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Of course to make any sort of useful Dynamic Object there’s a lot more involved, like managing the creation and recall of properties, handing conversions, invoking methods, etc. Which is why they provided ExpandoObject as a quick “I only need properties” alternative. But if you wanted to create a dynamic wrapper class, let’s say for an XML Document, you easily could do so using this new DynamicObject base class.