获取嵌套类的属性值始终为null

3
我有以下两个类:
public class Family
{
    public string ChildName { get; set; }
}

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Family Child { get; set; }

}

我有一个Employee类的实例,如下所示。
 Employee employee = new Employee();
 employee.Name = "Ram";
 employee.Id = 77;
 employee.Child = new Family() { ChildName = "Lava" };

我有一个方法,根据属性名称获取属性值,如下所示:
public static object GetPropertyValue(object src, string propName)
{
  string[] nameParts = propName.Split('.');

 if (nameParts.Length == 1)
  {
    return src.GetType().GetRuntimeProperty(propName).GetValue(src, null);
  }

foreach (String part in nameParts)
{
    if (src == null) { return null; }

    Type type = src.GetType();

    PropertyInfo info = type.GetRuntimeProperty(part);

    if (info == null)
    { return null; }

    src = info.GetValue(src, null);
   }
   return src;
}

在上述方法中,当我尝试获取嵌套类的属性值时,可以按以下方式操作:
GetPropertyValue(employee, "employee.Child.ChildName")  

或者
GetPropertyValue(GetPropertyValue(employee, "Family"), "ChildName"

因为type.GetRuntimeProperty(part)始终为空,所以不返回任何值。

有没有办法解决这个问题?

3个回答

2

你的问题在于这行代码:

foreach (String part in nameParts)

因为你正在遍历nameParts的每个部分,所以你也在遍历"employee",而它显然不是一个有效的属性。

尝试以下两种方法之一:

foreach (String part in nameParts.Skip(1))

或者像这样调用该方法:

GetPropertyValue(employee, "Child.ChildName")

(注意没有“employee.”,因为您已经传入了一个员工)

1
在这种情况下的问题是,当你分割字符串employee.Child.ChildName时,"employee"是第一部分。然而,employee不是源即Employee类的属性。
尝试这个:
public class Program
{
    public static void Main()
    {
        Employee employee = new Employee();
        employee.Name = "Ram";
        employee.Id = 77;
        employee.Child = new Family() { ChildName = "Lava" };
        GetPropertyValue(employee, "employee.Child.ChildName");

    }


    public class Family
    {
        public string ChildName { get; set; }
    }

    public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public Family Child { get; set; }

    }

    public static object GetPropertyValue(object src, string propName)
    {
        string[] nameParts = propName.Split('.');

        if (nameParts.Length == 1)
        {
            return src.GetType().GetRuntimeProperty(propName).GetValue(src, null);
        }
        nameParts = nameParts.Skip(1).ToArray();
        foreach (String part in nameParts)
        {
            if (src == null) { return null; }

            Type type = src.GetType();

            PropertyInfo info = type.GetRuntimeProperty(part);

            if (info == null)
            { return null; }

            src = info.GetValue(src, null);
        }
        return src;
    }

在这里,我已经跳过了字符串的第一部分,即"employee"。然而,你可以通过传递Child.ChildName来解决这个问题。


-1

这个问题已经存在了大约2年,但我为你的问题找到了另一个可行的解决方案,而且易于理解。如果在调用类构造函数时初始化对象,您可以使用点(.)表示法来分配或读取属性。例如 -

public class Family{
    public string ChildName { get; set; }
}

public class Employee{
    public int Id { get; set; }
    public string Name { get; set; }
    public Family Child { get; set; }

    public Employee(){
        Child = new Family();
    }
}

Employee emp = new Employee();
emp.Family.ChildName = "Nested calss attribute value";

你也可以在OP的例子中这样做(不需要构造函数),但是问题是为什么他的反射方法不起作用。 - PiMaker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接