.NET取得タイプの属性

1909 ワード

  • ソリューション
  • タイプ内のすべてのプロパティを反射的に取得します.
  • ネーミングスペース
  • を参照
     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;
       }