如 public class A { public int Pro1 { get; set; } public int Pro2 { get; set; } }
A a = new A(); 如何获取a.Pro1的字符串名称。即如何获得"Pro1"声明特性,msdn上查一下 C# code:
using System.Reflection;
Type t = typeof(A); foreach(PropertyInfo pi in t.GetProperties(BindingFlags.Instance | BidngFlags.Public)) { Console.WriteLine(pi.Name); }
正解 反射, System.Reflection.PropertyInfo[] propertys =对像.GetType().GetProperties(); foreach (System.Reflection.PropertyInfo info in propertys) { //info.Name 属性名称 } void Test() { A a