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 3 ways I can help you:
- 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.
- 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.
- Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.