我的代码存在问题。我做错了什么?
List<dynamic> list = new List<dynamic>();
var collumnBind = columnBindName(); // {id, n0, n1, etc...} Name of Columns Bind
list.Add(new ExpandoObject());
foreach (var c in collumnBind)
{
list[0].c = "something"; //not working in Datagrid
Console.Write(list[0].c); //in console i have "something"
list[0].id = "hello"; //working in Datagrid
}
答案 0 :(得分:2)
假设您要设置在自定义列表中定义的属性名称,columnBindName
是某种string
可枚举的,您可以使用ExpandoObject
实现IDictionary<string, object>
接口的功能您可以通过字符串名称获取/设置属性值
foreach (var c in collumnBind)
{
(list[0] as IDictionary<string, object>)[c] = "property value";
}