Things I didn’t know about expando objects

Yan Cui

I help clients go faster for less using serverless technologies.

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"));

   4:  

   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));

   4:  

   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) => { });

   2:  

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

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

INotifyPropertyChanged

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();

   2:  

   3: (expando as INotifyPropertyChanged).PropertyChanged += 

   4:     new PropertyChangedEventHandler(

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

   6:  

   7: // event fired

   8: expando.MyProperty = "hello";

   9:  

  10: // event fired

  11: expando.MyProperty = "world";

  12:  

  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.


 

Whenever you’re ready, here are 4 ways I can help you:

  1. If you want a one-stop shop to help you quickly level up your serverless skills, you should check out my Production-Ready Serverless workshop. Over 20 AWS Heroes & Community Builders have passed through this workshop, plus 1000+ students from the likes of AWS, LEGO, Booking, HBO and Siemens.
  2. If you want to learn how to test serverless applications without all the pain and hassle, you should check out my latest course, Testing Serverless Architectures.
  3. If you’re a manager or founder and want to help your team move faster and build better software, then check out my consulting services.
  4. If you just want to hang out, talk serverless, or ask for help, then you should join my FREE Community.

 


Leave a Comment

Your email address will not be published. Required fields are marked *