【问题标题】:Dynamically parsing through nested classes for values and attributes通过嵌套类动态解析值和属性
【发布时间】: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


    【解决方案1】:

    看看ObjectWalker

    它可以帮助您解析对象图并访问所有唯一元素。 如您所见,它仅存储一堆对象,而 Current 返回一个对象。

    您可以对此进行修改,以便 Walker 保存一个包含每个属性所需的所有数据的类(如 propertyinfo 对象或属性列表)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      • 2013-06-17
      相关资源
      最近更新 更多