Things I didn’t know about expando objects

Yan Cui

I help clients go faster for less using serverless technologies.

This article is brought to you by

Hookdeck: The Serverless Event Gateway

Hookdeck is a reliable and scalable serverless event gateway for sending, receiving, authenticating, transforming, filtering, and routing events between services in your event-driven architecture.

Learn more

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. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
  2. Do you want to know how to test serverless architectures with a fast dev & test loop? Check out my latest course, Testing Serverless Architectures and learn the smart way to test serverless.
  3. I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
  4. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

Leave a Comment

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