设置在运行时BrowsableAttribute的价值价值、BrowsableAttribute

2023-09-04 22:40:40 作者:情话难免七分假

我想设定 BrowsableAttribute 价值对一些 MyClass的在运行实例的属性:

I wanted to set a value of BrowsableAttribute for some of MyClass instance's properties at runtime:

 public class MyClass
{
     [Browsable(true)]
     public int P1 { get; set } 
     ...
}

请指教如何可以做到的,以及如何 BrowsableAttribute 添加到 MyClass的实例的属性在运行时如果这个属性不存在。

Please advise how it can be done as well as how to add BrowsableAttribute to a MyClass instance's property at runtime if this attribute doesn't exist.

推荐答案

您可以实现自定义类型说明符 - 它截获属性的道路上无论是使用它。因此,有效果,看起来像属性正在发生变化。

You can implement a custom type descriptor - which intercepts the the attribute on the way to whatever is using it. Thus having an effect which looks like the attribute is changing.

请参阅 - 第1部分,的第2部分