如题所示,在调用TestObject时,如何TestObject中BoolValue属性的默认值,描述文字,UITypeEditor等信息呢?
  public   class TestObject
    {      [DefaultValue(false),Description("描述")]
      public bool BoolValue
      {
          get;
          set;
      }    }

解决方案 »

  1.   

    反射http://www.cnblogs.com/jasonoiu/archive/2010/08/08/1795362.html
      

  2.   

    Attributes参考
    http://www.txwb.com/Article/wbcx/Easy/201104/94502.html
    http://topic.csdn.net/u/20100513/10/ae96022e-9f4f-49d4-97de-060346fcfd89.html
      

  3.   


                Type t = typeof(TestObject);
                PropertyInfo pi = t.GetProperty("BoolValue", BindingFlags.Public | BindingFlags.Instance);
                if (pi != null)
                {
                    foreach (Attribute att in pi.GetCustomAttributes(true))
                    {
                        if (att is DefaultValueAttribute)
                        {
                            Console.WriteLine("DefaultValue:{0}", ((DefaultValueAttribute)att).Value);
                        }
                        else if (att is DescriptionAttribute)
                        {
                            Console.WriteLine("Description:{0}", ((DescriptionAttribute)att).Description);
                        }
                    }
                }