【发布时间】:2017-08-31 06:27:11
【问题描述】:
一位大师,请说服我/我们这是怎么回事。
List<ExpandoObject> peopleList = new List<ExpandoObject>();
dynamic expandoObj1 = new ExpandoObject();
expandoObj1.id = 1;
expandoObj1.first = "fred";
expandoObj1.last = "krugger";
peopleList.Add(expandoObj1);
dynamic expandoObj2 = new ExpandoObject();
expandoObj2.id = 2;
expandoObj2.first = "george";
expandoObj2.last = "benson";
peopleList.Add(expandoObj2);
//test access the props
var expObj = expandoObj1;
var name = expObj.first;
var expObj2 = peopleList[0] as dynamic;
var name2 = expObj2.first;
IDictionary<string, object> expObj3 = peopleList[0] as ExpandoObject;
var name3 = expObj3["first"];
var expObj4 = peopleList[0] as ExpandoObject;
//var name4 = expObj4.first; //THIS DOESN'T WORK - ExpandoObject does not contain a definition for 'first' etc...
在所有情况下,左侧都是 System.Dynamic.ExpandoObject; 那么,为什么在第 4 种情况下 expObj4,我无法访问属性 expObj4.first ?
【问题讨论】:
-
因为变量没有声明为
dynamic,这是一个重要的部分!
标签: c#-4.0 dynamic expandoobject