

代码:
///动态修改特性 PropertyDescriptorCollection dataAttributes = TypeDescriptor.GetProperties(data);
Type displayType = typeof(BrowsableAttribute);
var fieldInfo = displayType.GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);
fieldInfo.SetValue(dataAttributes["Color"].Attributes[displayType], true);
需要注意的是,这里修改的是特性BrowsableAttribute 私有字段browsable,由于自动属性Browsable并没有set方法,只好如此了,如果有更好的方法,希望大神指导!
本文介绍了一种通过反射机制动态修改对象的BrowsableAttribute特性的方法,这种方法涉及到对私有字段的直接操作,以达到使自动属性Browsable在运行时可被控制的目的。
2608

被折叠的 条评论
为什么被折叠?



