ExpandoObject 的真正好处是什么? [英] What are the true benefits of ExpandoObject?
问题描述
ExpandoObject 类添加到 .NET 4 后,您可以在运行时将属性任意设置到对象上.
The ExpandoObject class being added to .NET 4 allows you to arbitrarily set properties onto an object at runtime.
与使用 Dictionary<string 相比,这有什么优势吗?object>
,甚至是 哈希表?据我所知,这只不过是一个哈希表,您可以使用更简洁的语法访问它.
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.
例如,这是为什么:
dynamic obj = new ExpandoObject();
obj.MyInt = 3;
obj.MyString = "Foo";
Console.WriteLine(obj.MyString);
确实比:
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 或多或少的真实用例: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"]);
层次越深,代码越丑.使用 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;
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);
}
}
另外,请记住,没有什么可以阻止您以动态方式接受事件参数.换句话说,您可以使用 EventHandler
而不是使用 EventHandler
,这会导致处理程序的第二个参数为 dynamic
.
Also, keep in mind that nothing is preventing you from accepting event arguments in a dynamic way. In other words, instead of using EventHandler
, you can use EventHandler<dynamic>
which would cause the second argument of the handler to be dynamic
.
这篇关于ExpandoObject 的真正好处是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!