【问题标题】:C# - Get class field property value [duplicate]C# - 获取类字段属性值
【发布时间】:2015-06-18 23:30:11
【问题描述】:

假设我有一个类,它由具有以下属性的其他类组成:

public class classA {
    public classAA classAA = new classAA();
    public classAB classAB = new classAB();

}

public class classAA {
    public string propertyA { get; set; }        
}

public class classAB {
    public string propertyB { get; set; }
}

我想遍历所有 classA 字段,然后遍历它们的属性并获取它们的值。 我创建了简单的控制台应用程序来测试它。

classA classA = new classA();
classA.classAA.propertyA = "A";
classA.classAB.propertyB = "B";

    foreach (FieldInfo memberAField in classA.GetType().GetFields()) {
        Console.WriteLine(memberAField.Name + " " + memberAField.MemberType + " " + memberAField.FieldType);
        foreach (PropertyInfo classAProperty in memberAField.FieldType.GetProperties()) {
            Console.WriteLine("name: " + classAProperty.Name);
            Console.WriteLine(" to value: " + classAProperty.GetValue(memberAField));
            }
        }

现在我不知道将什么传递给 GetValue 函数。我已经尝试了我能想到的一切,但我总是收到错误“对象与目标类型不匹配”。 我认为问题在于 memberAField 是 FieldInfo 对象,但是我怎样才能获得实际的 System object?

编辑:谢谢你们的回答!另外,我认为这不应该被标记为重复,因为我认为这是不同的问题。标题是相似的,是的。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您必须将要从中传递的实例传递给get the value

    首先你必须得到memberAField.GetValue(classA),这样你就有了字段的值,相当于classA.classAA。然后使用这个结果classAProperty.GetValue(memberAField.GetValue(classA))调用get value,相当于classA.classAA.PeropertyA

            Console.WriteLine(" to value: " + classAProperty.GetValue(memberAField.GetValue(classA)));
    

    完整代码:

    classA classA = new classA();
    classA.classAA.propertyA = "A";
    classA.classAB.propertyB = "B";
    
    foreach (FieldInfo memberAField in classA.GetType().GetFields()) {
        Console.WriteLine(memberAField.Name + " " + memberAField.MemberType + " " + memberAField.FieldType);
        object memberAValue = memberAField.GetValue(classA);
        foreach (PropertyInfo classAProperty in memberAField.FieldType.GetProperties()) {
            Console.WriteLine("name: " + classAProperty.Name);
            if (memberAValue == null)
                Console.WriteLine(" no value available");
            else
                Console.WriteLine(" to value: " + classAProperty.GetValue(memberAValue));
            }
        }
    

    【讨论】:

      【解决方案2】:

      GetValue 需要一个您正在为其获取属性值的对象实例,因此您只需在初始对象上调用 FieldInfo.GetValue

      foreach (FieldInfo memberAField in classA.GetType().GetFields()) {
          Console.WriteLine(memberAField.Name + " " + memberAField.MemberType + " " + memberAField.FieldType);
      
          object memberAValue = memberAField.GetValue(classA);  // instance to call GetValue on later.
      
          foreach (PropertyInfo classAProperty in memberAField.FieldType.GetProperties()) {
              Console.WriteLine("name: " + classAProperty.Name);
              Console.WriteLine(" to value: " + classAProperty.GetValue(memberAValue));
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-18
        • 1970-01-01
        • 2019-04-24
        • 1970-01-01
        相关资源
        最近更新 更多