问题:
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
来源:oschina
链接:https://my.oschina.net/stackoom/blog/4415170