Things I didn’t know about expando objects

I found out two interesting things about the ExpandoObject class introduced in C# 4 this bank holiday weekend:

1. you can specify custom events on them

2. it implements the INotifyPropertyChanged interface

Here are some quick demos to show you how to use these features:

Custom Events

To add a custom event is the same as adding a new property:

   1: // event handler

   2: expando.MyEvent = null;

   3: expando.MyEvent += new EventHandler((s, e) => Console.WriteLine("MyEvent fired"));


   5: expando.MyEvent(expando, new EventArgs());

Generic event handlers will work too:

   1: // generic event handler

   2: expando.MyGenericEvent = null;

   3: expando.MyGenericEvent += new EventHandler<MyEventArgs>((s, e) => Console.WriteLine(e.Message));


   5: expando.MyGenericEvent(expando, new MyEventArgs("Generic event fired"));

Because we’ve initialize the events with null, we’ll get a runtime exception if we try to invoke the event before it’s subscribed to (which is the same as normal custom events). So instead, we can use the same pattern I mentioned here, and initialize the custom events with an event handler that does nothing:

   1: expando.MyOtherEvent = new EventHandler((s, e) => { });


   3: // now you don't have to check if MyOtherEvent is null before invoking it

   4: expando.MyOtherEvent(expando, new EventArgs());


You should be familiar with the INotifyPropertyChanged interface already as it’s pretty damn important when it comes to data-binding in UI technologies such as WPF and Silverlight. So having a dynamic object you can easily bind with can help save you plenty of time and effort as anyone who’s ever had to write code to implement the interface will tell you!

   1: dynamic expando = new ExpandoObject();


   3: (expando as INotifyPropertyChanged).PropertyChanged += 

   4:     new PropertyChangedEventHandler(

   5:         (s, e) => Console.WriteLine("[{0}] was changed", e.PropertyName));


   7: // event fired

   8: expando.MyProperty = "hello";


  10: // event fired

  11: expando.MyProperty = "world";


  13: // event fired

  14: (expando as IDictionary<string, object>).Remove("MyProperty");

As you can see from the code snippet above, the PropertyChanged event is fired whenever a property is added, removed or changed.

It’s also worth noting that there is another dynamic class that was introduced alongside the ExpandoObject – the DynamicObject class, which offers you more granular control of what happens when someone tries to access/update a dynamically defined property or method.

Whilst the DynamicObject class doesn’t implement the INotifiyPropertyChanged interface itself,it is possible for you to extend it to implement the interface yourself, as described by this MSDN magazine article.

Enjoy what you’re reading? Subscribe to my newsletter and get more content on AWS and serverless technologies delivered straight to your inbox.

Yan Cui

I’m an AWS Serverless Hero and the author of Production-Ready Serverless. I have run production workload at scale in AWS for nearly 10 years and I have been an architect or principal engineer with a variety of industries ranging from banking, e-commerce, sports streaming to mobile gaming. I currently work as an independent consultant focused on AWS and serverless.

You can contact me via Email, Twitter and LinkedIn.

Hire me.

Check out my new course, Complete Guide to AWS Step Functions.

In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. Including basic concepts, HTTP and event triggers, activities, design patterns and best practices.

Get Your Copy