3

我有三个课程:

public class TestA
{
    public string Str1 { get; set; }
    public string Str2 { get; set; }
    public TestB TestB { get; set; }

    public TestA()
    {
        Str1 = "string1";
        Str2 = "string2";
        TestB = new TestB();
    }
}

public class TestB
{
    public string Str3 { get; set; }
    public string Str4 { get; set; }
    public TestC ObjTestC { get; set; }
    public TestB()
    {
        Str3 = "string3";
        Str4 = "string4";
        ObjTestC = new TestC();
    }
}

public class TestC
{
    public string Str5 { get; set; }
    public TestC()
    {
        Str5 = "string5";
    }
}

现在,我得到了所有的 PropertyInfo 并创建了一个新对象:

        //Get all the properties

        var prop = typeof(TestA).GetProperties();

        for (int i = 0; i < prop.Count(); i++)
        {
            var propertyInfo = prop[i];
            if (propertyInfo.PropertyType.Namespace != "System")
            {
                if (propertyInfo.PropertyType.IsGenericType &&
                    propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(List<>))
                {
                    Type itemType = propertyInfo.PropertyType.GetGenericArguments()[0]; // use this...
                    var listObjectProperties = itemType.GetProperties();
                    prop = prop.Union(listObjectProperties).ToArray();
                }
                else
                {
                    var childProp = propertyInfo.PropertyType.GetProperties();
                    prop = prop.Union(childProp).ToArray();
                }
            }
        }


        //Create Object
        TestA testA = new TestA();

现在,我需要调用每个属性的 getter 方法。我尝试了以下调用 TestA 类的属性的 getter。但是,在尝试调用 TestB 和 TestC 中的属性获取器时会引发错误:

        // Loop through all properties
        foreach (PropertyInfo propertyInfo in prop)
        {
            MethodInfo getterMethodInfo = propertyInfo.GetGetMethod();
            var obj=getterMethodInfo.Invoke(testA, null);
        }

请帮忙...

提前致谢

4

1 回答 1

7

检查这段代码,我希望该数组包含andprop上的所有属性,并且根本不涉及:TestATestBTestC

// Gets TestA's properties...
var prop = typeof(TestA).GetProperties();

// Then gets TestB's properties...
var childProp = propertyInfo.PropertyType.GetProperties();
prop = prop.Union(childProp).ToArray();

如果您想从所有三种类型中获取属性,您可能应该递归地进行。您不希望每次稍微更改这些对象的结构时都必须修改此循环。

但这不是真正的问题。

真正的问题是getterMethodInfo.Invoke(testA, null)针对 的属性调用TestB,但传递了 的实例TestA,因此出现错误。想想如果你不使用反射,你将如何获得这个值。您现有的代码基本上是这样做的:

var value = testA.Str3; // !!! there is no Str3 property on TestA !!!

要正常工作,您需要这样做:

var value = testA.TestB.Str3;

看看我们如何调用属性链来获得所需的值?首先TestA.TestB,然后TestB.Str3。如果您需要改用反射,则该代码需要执行相同的操作:

var obj = (object)testA;
obj = testBPropOnTestA.GetValue(obj, null);
var value = str3PropOnTestB.GetValue(obj, null);

因此,您不需要在集合中存储简单的实例PropertyInfo,而是需要存储PropertyInfos 的列表。

所以你最终的“获取值”循环看起来更像:

foreach (var propertyChain in prop)
{
    var obj = (object)testA;
    foreach (var property in propertyChain)
    {
        obj = property.GetValue(obj, null);
        if (obj == null) {
            break;
        }
    }
    Console.WriteLine("{0} = {1}",
        string.Join(".", propertyChain.Select(x => x.Name)),
        obj);
}

我将把如何解决第一个“gather PropertyInfos”循环的问题留给你。

于 2013-07-19T12:49:38.487 回答