我曾经能够在Net472中做到这一点,但是在Netcoreapp3.1中尝试执行相同操作时遇到错误
enter code here
BrowsableAttribute theDescriptorBrowsableAttribute = (BrowsableAttribute)theDescriptor.Attributes[typeof(BrowsableAttribute)];
FieldInfo browsablility = theDescriptorBrowsableAttribute.GetType().GetField("Browsable", BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Instance);
// Set the Descriptor's "Browsable" Attribute
browsablility.SetValue(theDescriptorBrowsableAttribute, isBrowsable);
浏览能力返回null。似乎现在是一个OnInitOnly,无法设置。
有什么想法吗?我只想根据属性使该项目可见或不可见。
我终于通过创建自定义属性并将该属性设置为true或false来解决该问题。并将该自定义属性分配给propertgird.BrowsableAttribute