问题描述
的ExpandoObject类被添加到.NET 4中,您可以随意设置属性到对象在运行时。
The ExpandoObject class being added to .NET 4 allows you to arbitrarily set properties onto an object at runtime.
有什么优势,这种过度使用<$c$c>Dictionary<string,object>$c$c>,还是真的连一个哈希表?据我所知,这只不过是一个哈希表,您可以使用稍微更简洁的语法进行访问。
Are there any advantages to this over using a Dictionary<string,object>
, or really even a Hashtable? As far as I can tell, this is nothing but a hash table that you can access with slightly more succinct syntax.
例如,为什么是这样的:
For example, why is this:
dynamic obj = new ExpandoObject();
obj.MyInt = 3;
obj.MyString = "Foo";
Console.WriteLine(obj.MyString);
真的好,还是完全不同的,比:
Really better, or substantially different, than:
var obj = new Dictionary<string, object>();
obj["MyInt"] = 3;
obj["MyString"] = "Foo";
Console.WriteLine(obj["MyString"]);
什么真正的优点是通过使用ExpandoObject而不是只使用一个任意字典式上涨,比不是很明显,您使用的类型将是在运行时确定等。
What real advantages are gained by using ExpandoObject instead of just using an arbitrary dictionary type, other than not being obvious that you're using a type that's going to be determined at runtime.
推荐答案
自从我写了MSDN文章你指的是,我想我必须回答这个问题。
Since I wrote the MSDN article you are referring to, I guess I have to answer this one.
首先,我预计这个问题,这就是为什么我写了一篇博客文章,显示了或多或少的实际使用情况进行ExpandoObject:Dynamic在C#4.0:介绍ExpandoObject 。
First, I anticipated this question and that's why I wrote a blog post that shows a more or less real use case for ExpandoObject: Dynamic in C# 4.0: Introducing the ExpandoObject.
不久,ExpandoObject可以帮助您创建复杂的分层对象。例如,假设你有一个字典中的字典:
Shortly, ExpandoObject can help you create complex hierarchical objects. For example, imagine that 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"]);
更深的层次,在丑陋的code。随着ExpandoObject它保持优雅和可读性。
The deeper is the hierarchy, the uglier is the code. With ExpandoObject it stays elegant and readable.
dynamic expando = new ExpandoObject();
expando.Address = new ExpandoObject();
expando.Address.State = "WA";
Console.WriteLine(expando.Address.State);
其次,因为它已经指出,ExpandoObject实现INotifyPropertyChanged接口,为您提供了更多的控制权比一本字典的属性。
Second, as it was already pointed out, ExpandoObject implements INotifyPropertyChanged interface which gives you more control over properties than a dictionary.
最后,你可以添加事件ExpandoObject喜欢这里:
Finally, you can add events to ExpandoObject like 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;
if (e != null)
{
e(d, new EventArgs());
}
// We could also fire it with...
// d.MyEvent(d, new EventArgs());
// ...if we knew for sure that the event is non-null.
}
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);
}
}
这篇关于什么是ExpandoObject的真正好处是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!