【发布时间】:2011-11-10 15:17:48
【问题描述】:
我有几个模型,其中包括嵌套类和列表。许多班级成员都有我也必须读入的属性。
我正在寻找一种动态方式(可能通过 Linq 或反射)来检索指定模型中所有通用对象的值和属性。
欢迎提出任何建议。
编辑:
按照 x0r 的建议使用 ObjectManager,我可以看到所有数据。此问题的其余部分需要成员注释。有什么方法可以复制每个类成员的 PropertyInfo 吗?
ObjectWalker objectWalker = new ObjectWalker(objectToValidate);
foreach (Object o in objectWalker)
{
if (isGeneric(o.GetType()))
{
PropertyInfo property = o.GetType().GetProperty(o); // <-- This does not work... Need to obtain annotations somehow
object[] Attributes = property.GetCustomAttributes(typeof(Attribute), true);
foreach (Attribute attribute in Attributes)
{
// Annotations processing goes here
}
}
}
【问题讨论】:
标签: c# linq parsing class reflection