【发布时间】:2014-08-07 02:57:13
【问题描述】:
我将动态对象的属性名称分配为字符串形式的整数。 int 值表示我正在使用的数据库中的 int ID。但是我被困在如何检索分配给属性的值,如下所示:
dynamic test = new ExpandoObject()
IDictionary<string, object> proxyFiler = test as IDictionary<string, object>;
proxyFiler["four"] = 4;
proxyFiler["5"] = 5;
int r = test.four; // Works
int s = test.5; // Doesn't work
读取数据库的方法将返回一个“int”,我希望能够访问具有该属性名称的属性值。
对此进行扩展:如果我想做一个 linq 查询以根据属性名称对动态对象列表进行排序怎么办?在这种情况下,我需要获取我作为字符串检索的 propertyName,例如“15”:
return disorderedList.OrderBy(o => o.propertyName).ToList();
有没有人知道这个问题的简单解决方案,或者您推荐一种不同的方法?谢谢。
【问题讨论】:
-
为什么第二个例子会起作用?
value未使用。 -
为什么一定要说
test.5而不是proxyFiler["5"]?
标签: c# linq expandoobject idictionary