Problem
The ExpandoObject class in.NET 4 allows you to set attributes on an object at runtime arbitrarily.
Are there any advantages to utilizing this instead of a Dictionarystring, object> or even a Hashtable? This appears to be nothing more than a hash table that can be accessed with slightly more concise syntax, as far as I can determine.
For instance, why is it that:
dynamic obj = new ExpandoObject();
obj.MyInt = 3;
obj.MyString = "Foo";
Console.WriteLine(obj.MyString);
Really better than, or significantly different from:
var obj = new Dictionary<string, object>();
obj["MyInt"] = 3;
obj["MyString"] = "Foo";
Console.WriteLine(obj["MyString"]);
What are the real benefits of using ExpandoObject instead of an arbitrary dictionary type, other from the fact that it’s not evident that you’re using a type that will be determined at runtime?
Asked by Reed Copsey
Solution #1
I assume I’ll have to respond to this one because I wrote the MSDN article you’re referring to.
First, I anticipated this question, which is why I published a blog article demonstrating a more or less real-world ExpandoObject use case: In C# 4.0, there’s a new feature called dynamic. The ExpandoObject is a new type of object.
ExpandoObject, in a nutshell, may assist you in creating complicated hierarchical objects. Consider the case when you have a dictionary within a dictionary.
Dictionary<String, object> dict = new Dictionary<string, object>();
Dictionary<String, object> address = new Dictionary<string,object>();
dict["Address"] = address;
address["State"] = "WA";
Console.WriteLine(((Dictionary<string,object>)dict["Address"])["State"]);
The code becomes uglier as the structure grows deeper. It keeps sleek and readable with ExpandoObject.
dynamic expando = new ExpandoObject();
expando.Address = new ExpandoObject();
expando.Address.State = "WA";
Console.WriteLine(expando.Address.State);
Second, as previously stated, ExpandoObject implements the INotifyPropertyChanged interface, which provides you with more property control than a dictionary.
Finally, events can be added to ExpandoObject, as shown here:
class Program
{
static void Main(string[] args)
{
dynamic d = new ExpandoObject();
// Initialize the event to null (meaning no handlers)
d.MyEvent = null;
// Add some handlers
d.MyEvent += new EventHandler(OnMyEvent);
d.MyEvent += new EventHandler(OnMyEvent2);
// Fire the event
EventHandler e = d.MyEvent;
e?.Invoke(d, new EventArgs());
}
static void OnMyEvent(object sender, EventArgs e)
{
Console.WriteLine("OnMyEvent fired by: {0}", sender);
}
static void OnMyEvent2(object sender, EventArgs e)
{
Console.WriteLine("OnMyEvent2 fired by: {0}", sender);
}
}
Also, keep in mind that there’s nothing stopping you from accepting event arguments dynamically. To put it another way, instead of using EventHandler, you can use EventHandlerdynamic>, which makes the handler’s second argument dynamic.
Answered by Alexandra Rusina
Solution #2
One advantage is that it can be used in binding contexts. The TypeDescriptor system will take up the dynamic characteristics in data grids and property grids. Furthermore, because WPF data binding will recognize dynamic properties, WPF controls will be able to bind to an ExpandoObject more easily than a dictionary.
In some circumstances, interoperability with dynamic languages, which would anticipate DLR attributes rather than dictionary entries, may be a consideration.
Answered by itowlson
Solution #3
For me, the main benefit of XAML is how easy it makes data binding:
public dynamic SomeData { get; set; }
…
SomeData.WhatEver = "Yo Man!";
…
<TextBlock Text="{Binding SomeData.WhatEver}" />
Answered by bjull
Solution #4
The first reason that comes to me is interoperability with other languages based on the DLR. Because it isn’t an IDynamicMetaObjectProvider, you can’t pass them a Dictionarystring, object>. Another advantage is that it implements INotifyPropertyChanged, which means that it has further benefits in the databinding environment of WPF beyond what DictionaryK,V> can deliver.
Answered by Drew Marsh
Solution #5
It’s all about the programmers’ comfort. With this object, I can foresee developing rapid and dirty programs.
Answered by ChaosPandion
Post is based on https://stackoverflow.com/questions/1653046/what-are-the-true-benefits-of-expandoobject