ExpandoObject的真正好处是什么?

我们两清 提交于 2020-08-15 18:33:45

问题:

The ExpandoObject class being added to .NET 4 allows you to arbitrarily set properties onto an object at runtime. 添加到.NET 4的ExpandoObject类允许您在运行时任意设置对象的属性。

Are there any advantages to this over using a Dictionary<string, object> , or really even a Hashtable ? 使用Dictionary<string, object>或者甚至是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"]);

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. 使用ExpandoObject而不仅仅使用任意字典类型可以获得什么真正的优势,除了不明显你正在使用将在运行时确定的类型。


解决方案:

参考一: https://stackoom.com/question/6w22/ExpandoObject的真正好处是什么
参考二: https://oldbug.net/q/6w22/What-are-the-true-benefits-of-ExpandoObject
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!