C# 通过 SetPropertyVisible 动态设置某个属性的可见性,无法生效?
public static void SetPropertyVisible(object entity, string attrName, bool visible)
{
Type type = typeof(BrowsableAttribute);
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(entity);
AttributeCollection attrs = props[attrName].Attributes;
FieldInfo info = type1.GetField("browsable", BindingFlags.CreateInstance | BindingFlags.NonPublic | BindingFlags.Instance);
info.SetValue(attrs[type], visible);
}
比如要动态设置可见性的属性如下:
private Color textColor;
[Browsable(true)]
public Color TextColor
{
get { return textColor; }
set { textColor = value; }
}
设置方法:SetPropertyVisible(this,"TextColor",false); 但是执行后,该属性依然是可见的,没有被隐藏,如何使不可见?