Quantcast
Viewing all articles
Browse latest Browse all 10

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

   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.

Liked this article? Support me on Patreon and get direct help from me via a private Slack channel or 1-2-1 mentoring.

Image may be NSFW.
Clik here to view.

Image may be NSFW.
Clik here to view.

Hi, my name is Yan Cui. I’m an AWS Serverless Hero and the author of Production-Ready Serverless. I specialise in rapidly transitioning teams to serverless and building production-ready services on AWS.

Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.

You can contact me via Email, Twitter and LinkedIn.

Hire me.

The post Things I didn’t know about expando objects appeared first on theburningmonk.com.


Viewing all articles
Browse latest Browse all 10

Trending Articles