2016-04-22 170 views
0
dynamic contacts = new List<dynamic>(); 
contacts.Add(new ExpandoObject()); 
contacts[0].Name = "Patrick Hines" ; 
contacts[0].Phone = "206-555-0144" ; 

想做如下。將動態值賦給動態對象

dynamic contacts = new List<dynamic>(); 
contacts.Add(new ExpandoObject()); 
contacts[0]. + "Name" = "Patrick Hines" ; 
contacts[0]. + "Phone" = "206-555-0144" ; 

回答

0

這個怎麼樣

var contacts = new List<Dictionary<string, string>>(); 
contacts.Add(new Dictionary<string, string>()); 

contacts[0]["Name"] = "Patrick Hines"; 
contacts[0]["Phone"] = "206-555-0144"; 
+0

謝謝你這麼多。 – user3197034

0

您可以按名稱,如果你訪問它作爲一個IDictionary<string, object>訪問ExpandoObject的成員。將你的清單設爲List<IDictionary<string, object>>,這將允許你做這樣的事情:

var contacts = new List<IDictionary<string, object>> { new ExpandoObject() }; 
contacts[0]["Name"] = "Patrick Hines"; 

var asDynamic = contacts as dynamic; 
asDynamic[0].Phone = "206-555-0144"; 
+0

非常感謝。完成! – user3197034