嵌套类的Get属性值始终为null

本文关键字:null 属性 Get 嵌套 | 更新日期: 2023-09-27 18:00:08

我有以下两个类

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)始终为null。

有什么办法解决这个问题吗?

嵌套类的Get属性值始终为null

您的问题在于这一行:

foreach (String part in nameParts)

因为您在对nameParts的每个部分进行迭代,所以您也在对"employee"进行迭代,这当然不是一个有效的属性。

试试这个:

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

或者调用这样的方法:

GetPropertyValue(employee, "Child.ChildName")

(请注意,没有"employee.",因为您已经传入了一名员工)

这种情况下的问题是,当您拆分字符串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

来解决问题

这个问题已经有2年的历史了,但我为您找到了另一个简单易懂的工作解决方案。如果在调用calss构造函数时初始化对象,则可以使用点(.)表示法来分配或读取属性。示例-

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";