Expando Objects!

I recently had a need to use objects that had properties I could define or remove at runtime in C#.

I’m embarrassed to admit that I actually didn’t know that such a thing existed, because I don’t usually use the language in that capacity. It generally doesn’t make sense to, either, because you lose a lot of the power that the compiler and type checking and whatnot provides you. But, in this case, I was reading a file that would roughly correspond to objects of different “shapes”. All I had to do was take this information from the file and pack it up in a tidy little object to ship it somewhere else. The “somewhere else” in question was responsible for long-term persistence of the data and it truly did not care what the object looked like. It’ll store anything! The easiest, fastest, and most straightforward way to handle this was to read everything in and just assign nice key/value pairs on something. I was getting ready to write some utilities in some language that just supports this, and deal with passing information around somehow, when I thought “There’s no way C# doesn’t do something like this already.”

Turns out I was correct: Enter the System.Dynamic.ExpandoObject.

Firstly, that’s a great name. Secondly, this is just exactly what I was looking for. Since I didn’t know about it, I’m going to assume that others don’t, either. Maybe this will be helpful!

My initial thought was “Hey, this looks quite a bit like a Dictionary,” but the syntax is more convenient for this particular use case. Plus, it has some other useful features that we’ll get to in just a little bit.

If you’ve looked around this blog a bit, you have probably noticed that I’m pretty familiar with JavaScript. At a very superficial level, these ExpandoObjects appear to operate in a similar manner to working with objects in JavaScript. You can pass them around as a parameter by just calling them “dynamic” in the arguments for the method. You’ve also given up the compiler safety net, so whatever you try to call on the object better exist or you’re in for an exception.

If you wanted to try building an ExpandoObject for yourself, you might start off with these kinds of utilities (or you’d make some extensions to existing interfaces, you do you):

private static void AddProperty(string id, object val, object target)
    ((IDictionary<String, Object>)target).Add(id, val);

private static void RemoveProperty(string id, object target)
    ((IDictionary<String, Object>)target).Remove(id);

Further operations pretty much follow that same format. But the ExpandoObject syntax is so much more convenient, and it already exists for you. Whatever object you create ends up being something you can ship around to other languages that support the Dynamic Language Runtime! I didn’t have a use for that, but maybe you do! Now, go off and bypass your compiler checking by using some ExpandoObjects. Have fun!

Published by Joe

I'm a software developer from Minnesota.

%d bloggers like this: