通过反射获取类的所有属性

    xiaoxiao2025-06-12  25

    class Program { static void Main(string[] args) { var prop = GetpropertyInfoArray(); foreach (var item in prop) { Console.WriteLine(item.Name+" "+item.PropertyType+" "+item.GetValue(new A(),null)); } Console.Read(); } private static PropertyInfo[] GetpropertyInfoArray() { Type type = typeof(A); object obj = Activator.CreateInstance(type); return type.GetProperties(); } } class A { public int E { get; set; } = 123; public string B { get; set; } = "234234"; public string C { get; set; } = "564645"; public int D { get; set; } = 4234234; }

    结果:

    E System.Int32 123 B System.String 234234 C System.String 564645 D System.Int32 4234234

    public void PrintInstanceInfor(object t) { //获取所有方法 System.Reflection.MethodInfo[] methods = t.GetMethods(); //获取所有成员 System.Reflection.MemberInfo[] members = t.GetMembers(); //获取所有属性 System.Reflection.PropertyInfo[] properties = t.GetProperties(); }

    转载请注明原文地址: https://ju.6miu.com/read-1299870.html
    最新回复(0)