C# 反射 获取类的所有属性

结论:默认情况遍历属性时,是不会遍历到静态成员的。

测试代码:

public class Class11
{
    public void test1()
    {
        c1 model = new c1();
        model.name = "name1";

        Type t = model.GetType();
        PropertyInfo[] pArray = t.GetProperties();
        Array.ForEach<PropertyInfo>(pArray, p =>
        {
            Console.WriteLine(p.Name);
        });
    }

    public class c1
    {
        public static string StrIds = "12333";//这个字段 反射时不会遍历到
        public string name { get; set; }
    }
}

输出的结果:

猜你喜欢

转载自www.cnblogs.com/guxingy/p/12743581.html