小编Kev*_*oss的帖子

C# 带有子类的 PropertyInfo 的 GetValue

首先,抱歉我的英语不好...我希望你能理解我想说的话。

我有一个小代码问题,我需要获取类属性的值。(这不是我的完整项目,而是我想做的事情的概念。用这个简单的代码,我被阻止了。)

有代码:(此示例工作正常。)

using System;
using System.Reflection;

class Example
{
    public static void Main()
    {
        test Group = new test();
        BindingFlags bindingFlags = BindingFlags.Public |
                                    BindingFlags.NonPublic |
                                    BindingFlags.Instance |
                                    BindingFlags.Static;
        Group.sub.a = "allo";
        Group.sub.b = "lol";

        foreach (PropertyInfo property in Group.GetType().GetField("sub").FieldType.GetProperties(bindingFlags))
        {
            string strName = property.Name;
            Console.WriteLine(strName + " = " + property.GetValue(Group.sub, null).ToString());
            Console.WriteLine("---------------");
        }
    }
}

public class test
{
    public test2 sub = new test2();
}

public class test2
{
    public string a { get; set; } …
Run Code Online (Sandbox Code Playgroud)

c# foreach getproperty getvalue

5
推荐指数
1
解决办法
1255
查看次数

标签 统计

c# ×1

foreach ×1

getproperty ×1

getvalue ×1