通过反射C#获取嵌套的属性值 [英] Get nested property values through reflection C#

查看:72
本文介绍了通过反射C#获取嵌套的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码.

课程:

public class AlloyDock
{
    public int Left { get; set; }
    public int Right { get; set; }
}

public class Charger
{
    public int Left { get; set; }
    public int Right { get; set; }
}
public class VehicleControlTest
{
    public Charger Charger1 { get; set; }
}
public class BasicControlTest
{
    public AlloyDock AlloyDock1 { get; set; }
}
class Appointment
{
    public BasicControlTest BasicControlTest1 { get; set; }
    public VehicleControlTest VehicleControlTest1 { get; set; }
}

主要功能:

        var obj = new Appointment();
        obj.BasicControlTest1 = new BasicControlTest();
        obj.BasicControlTest1.AlloyDock1 = new AlloyDock();
        obj.BasicControlTest1.AlloyDock1.Left = 1;
        obj.BasicControlTest1.AlloyDock1.Right = 2;

        obj.VehicleControlTest1 = new VehicleControlTest();
        obj.VehicleControlTest1.Charger1 = new Charger();
        obj.VehicleControlTest1.Charger1.Left = 3;
        obj.VehicleControlTest1.Charger1.Right = 4;


        var parentProperties = obj.GetType().GetProperties();
        foreach (var prop in parentProperties)
        {
            // Get Main objects inside each test type.
            var mainObjectsProperties = prop.PropertyType.GetProperties();
            foreach (var property in mainObjectsProperties)
            {
                var leafProperties = property.PropertyType.GetProperties();
                foreach (var leafProperty in leafProperties)
                {
                    Console.WriteLine("{0}={1}", leafProperty.Name, leafProperty.GetValue(obj, null));
                }
            }
        }

我想获取叶节点的属性名称和值.我能够获得名称,但是当我尝试获得价值时(分别为1,2,3,4).我遇到错误了.

I want to get property name and value of leaf node. I am able to get name but when I try to get value (1,2,3,4 respectively). I am getting below error.

对象与目标类型不匹配.

我只是想解决这个问题.有人可以帮我吗?

I am just banging my head to solve this problem. Can anybody help me with that.

推荐答案

将对象实例传递给 GetValue 方法时,您需要传递正确类型的实例:

When passing an object instance to the GetValue method, you need to pass the instance of the correct type:

// 1st level properties
var parentProperties = obj.GetType().GetProperties();
foreach (var prop in parentProperties)
{
    // get the actual instance of this property
    var propertyInstance = prop.GetValue(obj, null);

    // get 2nd level properties
    var mainObjectsProperties = prop.PropertyType.GetProperties();

    foreach (var property in mainObjectsProperties)
    {
        // get the actual instance of this 2nd level property
        var leafInstance = property.GetValue(propertyInstance, null);

        // 3rd level props
        var leafProperties = property.PropertyType.GetProperties();

        foreach (var leafProperty in leafProperties)
        {
            Console.WriteLine("{0}={1}",
                leafProperty.Name, leafProperty.GetValue(leafInstance, null));
        }
    }
}

您可以递归执行此操作以简化(概括)整个过程:

You might do this recursively to simplify (generalize) the whole thing:

static void DumpObjectTree(object propValue, int level = 0)
{
    if (propValue == null)
        return;

    var childProps = propValue.GetType().GetProperties();
    foreach (var prop in childProps)
    {
        var name = prop.Name;
        var value = prop.GetValue(propValue, null);

        // add some left padding to make it look like a tree
        Console.WriteLine("".PadLeft(level * 4, ' ') + "{0}={1}", name, value);

        // call again for the child property
        DumpObjectTree(value, level + 1);
    }
}

// usage: DumpObjectTree(obj);

这篇关于通过反射C#获取嵌套的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆