条件“可浏览”属性

有没有办法使“可浏览”属性成为条件,因此应用它的属性有时会出现在属性页面中,有时不会出现?
谢谢 :)

没有简单的方法。

您可以通过实现ICustomTypeDescriptor来解决这个问题。 这是一篇关于实现ICustomTypeDescriptor的好文章。

或者,您可以将自己的ControlDesigner与类关联,并覆盖PreFilterProperties方法以添加或删除在属性网格中查看的属性。

从属性网格中删除某些属性。

我不确定这适用于您的情况,但您可以通过调用下面的函数在运行时调整“可浏览”装饰。

///  /// Set the Browsable property. /// NOTE: Be sure to decorate the property with [Browsable(true)] ///  /// Name of the variable /// Browsable Value private void setBrowsableProperty(string strPropertyName, bool bIsBrowsable) { // Get the Descriptor's Properties PropertyDescriptor theDescriptor = TypeDescriptor.GetProperties(this.GetType())[strPropertyName]; // Get the Descriptor's "Browsable" Attribute BrowsableAttribute theDescriptorBrowsableAttribute = (BrowsableAttribute)theDescriptor.Attributes[typeof(BrowsableAttribute)]; FieldInfo isBrowsable = theDescriptorBrowsableAttribute.GetType().GetField("Browsable", BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Instance); // Set the Descriptor's "Browsable" Attribute isBrowsable.SetValue(theDescriptorBrowsableAttribute, bIsBrowsable); } 

您可以通过提供自定义类型模型来完成此操作; 在最简单的级别,您可以为从ExpandableObjectConverter派生的类型提供自定义TypeDescriptor ,并且只需在奇思妙想中包含/排除给定属性 – 但这仅适用于PropertyGrid – 由属性页面使用。 更复杂的方法是使用ICustomTypeDescriptor / TypeDescriptionProvider – 这可以在DataGridView东西中工作

我发现这是为了寻找一种方法来声明某些成员在IntelliSense中可见或隐藏,并且能够在编译时为所有需要隐藏的内容更改一次。 我不知道这是不是你在找什么,但我找到了一个问题的答案……认为分享不会有什么坏处。

我设置了一个条件编译符号(在项目属性的Build选项卡中找到)IS_VIS(如果你想要显示某些成员,则值为true,如果你想隐藏它们则为false)然后:

 #if IS_VIS public const System.ComponentModel.EditorBrowsableState isVis = ComponentModel.EditorBrowsableState.Always; #else public const System.ComponentModel.EditorBrowsableState isVis = ComponentModel.EditorBrowsableState.Never; #endif 

然后在属性中引用isVis变量:

 [EditorBrowsable(isVis)] public string myMethod... 

我在VB中这样做了,这被匆忙转换为c#。 如果某些东西不能正常工作,请告诉我。

作为@ neoikon上面的答案的改进,为了避免Ganesh在评论中提到的exception,这里有一个使用generics来获取类型的版本:

  ///  /// Set the Browsable property. /// NOTE: Be sure to decorate the property with [Browsable(true)] ///  /// Name of the variable /// Browsable Value private void SetBrowsableProperty(string strPropertyName, bool bIsBrowsable) { // Get the Descriptor's Properties PropertyDescriptor theDescriptor = TypeDescriptor.GetProperties(typeof(T))[strPropertyName]; // Get the Descriptor's "Browsable" Attribute BrowsableAttribute theDescriptorBrowsableAttribute = (BrowsableAttribute)theDescriptor.Attributes[typeof(BrowsableAttribute)]; FieldInfo isBrowsable = theDescriptorBrowsableAttribute.GetType().GetField("Browsable", BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Instance); // Set the Descriptor's "Browsable" Attribute isBrowsable.SetValue(theDescriptorBrowsableAttribute, bIsBrowsable); } 

然后,您还可以添加一个采用实例的版本:

  ///  /// Set the Browsable property. /// NOTE: Be sure to decorate the property with [Browsable(true)] ///  /// An instance of the object whose property should be modified. /// Name of the variable /// Browsable Value private void SetBrowsableProperty(T obj, string strPropertyName, bool bIsBrowsable) { SetBrowsableProperty(strPropertyName, bIsBrowsable); } 

用法:

  class Foo { [Browsable(false)] public string Bar { get; set; } } void Example() { SetBrowsableProperty("Bar", true); Foo foo = new Foo(); SetBrowsableProperty(foo, "Bar", false); }