在设计时通过反射改变属性的特性

   1: public class A

   2:      {
   3:          public string Name { get; set; }
   4:   
   5:          public string Age { get; set; }
   6:   
   7:          public bool IsOk { get; set; }
   8:   
   9:          [Browsable(false)]
  10:          public bool Visiable { get; set; }
  11:      }
  12:   
  13:  private void button1_Click(object sender, EventArgs e)
  14:          {
  15:              A a = new A();
  16:              Type type = typeof(BrowsableAttribute);
  17:   
  18:              PropertyDescriptorCollection props = TypeDescriptor.GetProperties(a);
  19:              AttributeCollection attrs = props["Visiable"].Attributes;
  20:   
  21:              FieldInfo fld = type.GetField("browsable", BindingFlags.Instance | BindingFlags.NonPublic);
  22:              fld.SetValue(attrs[type], true);
  23:   
  24:              this.propertyGrid1.SelectedObject = a;
  25:          }

posted @ 2013-03-05 14:15  一马  阅读(158)  评论(0编辑  收藏  举报