.NET 获取类型中的属性

 

  • 解决方案
     通过反射的方式获取类型中的所有属性。
  • 引用命名空间
 using System.Reflection;
  • 实体类
  public class User
  {
        private string id;
        public string Id { get { return id; } set { id = value; } }

        private string name;
        public string Name { get { return name; } set { name = value; } }
  }
  • 获取方法
   private PropertyInfo[] GetPropertyInfoArray()
   {
        PropertyInfo[] props = null;
        try
        {
             Type type = typeof(User);
             object obj = Activator.CreateInstance(type);
             props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
        }
        catch (Exception ex)
        { }
        return props;
   }
posted @ 2013-07-04 17:21  M守护神  阅读(1589)  评论(0编辑  收藏  举报