动态地为动态对象赋值

时间:2016-04-22 03:20:47

标签: c#-4.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" ;

2 个答案:

答案 0 :(得分: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";

答案 1 :(得分:0)

如果您以ExpandoObject的身份访问IDictionary<string, object>,则可以按名称访问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"; ,这样您就可以执行以下操作:

<fieldset id="field_1">
.....
<button class="action-button" id="button1" >Next</button>
</fieldset>

<fieldset id="field_2">
.....
 <input type="button" name="previous" class="previous action-button" value="Previous" />
<button class="action-button" id="button2" >Next</button>
<button class="action-button"  >Save&Exit</button>
</fieldset>

.....

<fieldset id="field_10">
.....
</fieldset>