Ironing code, geek t-shirts and even presentations!


The dynamic Keyword Part 2 – IDynamicObject

This is the second part of my series about the new dynamic keyword in C# 4 (Read the first post here).

As I talked on the first post, the dynamic keyword does the dirty reflection work for you. This is very nice, but it’s like moving forward from a desktop computer to a 10kg laptop – you get to be more dynamic, but it’s still heavy to move around…

The first laptop - heavy!

The dynamic keyword holds a greater power inside, and this is what I’m going to talk about today – the IDynamicObject interface.

What is the IDynamicObject interface?

When we declare an object as dynamic and execute actions upon it, we actually use a default IDynamicObject implementation.

The IDynamicObject interface has a few methods, the important ones (for this post) are – Invoke, GetMember and SetMember.
The idea is very simple -

dynamic myObject = GetIDynamicObject();
myObject.SomeProperty = "Hello"; // This calls IDynamicObject.SetMember
Console.WriteLine(myObject.SomeProperty); // This calls IDynamicObject.GetMember
myObject.RunSomething(); // This calls IDynamicObject.Invoke

This means that we can implement our own behavior for the dynamic invocation! let’s see what we can do with that…

What can I do with that?

If we implement our own IDynamicObject, we can ease the pain of various daily operations. For example, we can develop an IDynamicObject that will give us the ability to use un-typed datasets as if they were typed. Another example can be an implementation that will enable us to dot through an xml file, with no need of XmlDocument, xpath and their friends. I’m sure there are tons of other uses that can and will be implemented.

Sample IDynamicObject implementation – Working with un-typed datasets

The first thing we’ll do is to create an extension method to the System.Data.DataSet class:

public static class Extensions
public static dynamic AsDynamic(this DataSet dataSet)
return new DataSetDynamicCaller(dataSet);
When we call DataSet.AsDynamic(), we’ll receive a DataSetDynamicCaller class in return, as a dynamic type object. 

Now what we need to do, is to generate classes that dynamically access the dataset objects. This is a very easy process as you will see in just a moment.

So let’s create the DataSetDynamicCaller class:

public class DataSetDynamicCaller : DynamicObject
DataSet dataset;

public DataSetDynamicCaller(DataSet dataset)
this.dataset = dataset;

public override object GetMember(System.Scripting.Actions.GetMemberAction info)
return new dataset.Tables[info.Name];

This is it… Now to the explanation…

Firstly, I inherit from DynamicObject – this is the basic implementation to the IDynamicObject interface (download link is at the end of the post). As I understand, the DynamicObject class will be a part of the System.Dynamic namespace in C# 4, so you won’t need this file on the final version. I won’t deep dive into this implementation, it’s a subject to another post.

Secondly, The DataSetDynamicCaller class is very simple – we get the untyped dataset in the constructor and save it in a private member. Then, whenever the user asks for a property, we return a DataTable that matches the name of the requested member.
This will give us the ability to call a table directly. For example:

DataSet ds = new DataSet();

dynamic dynDS = ds.AsDynamic();

DataTable tbl = dynDS.MyTable; // This will work!

It’s as simple as that!

I attach the class I’ve written (the link is at the end of the post). The sample contains two more classes and enables you to use the un-typed dataset even more easily. For instance, calling dynDS.MyTable.GetRow(0).MyColumn will return the value of the MyColumn column from the first row of the MyTable table…

In conclusion, the IDynamicObject interface opens a large amount of possibilities for improving our everyday work in a lot of aspects. It starts from accessing untyped datasets as if they were typed and ends where your imaginations does. This is what they mean when they say dynamic capabilities!



All the best,

kick it on
Shout it

Add comment