BrowsableAttribute 类

指定一个属性 (Property) 或事件是否应显示在“属性”窗口中。

**命名空间:**System.ComponentModel
**程序集:**System(在 system.dll 中)

语法

声明
<AttributeUsageAttribute(AttributeTargets.All)> _
Public NotInheritable Class BrowsableAttribute
    Inherits Attribute
用法
Dim instance As BrowsableAttribute
[AttributeUsageAttribute(AttributeTargets.All)] 
public sealed class BrowsableAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::All)] 
public ref class BrowsableAttribute sealed : public Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.All) */ 
public final class BrowsableAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.All) 
public final class BrowsableAttribute extends Attribute

备注

可视化设计器通常在“属性”窗口中显示没有可浏览属性 (Attribute) 的成员,或使用值 trueBrowsableAttribute 构造函数标记的成员。这些成员可以在设计时进行修改。使用值 falseBrowsableAttribute 构造函数标记的成员不适合在设计时进行编辑,因此,它们不会在可视化编辑器中显示。默认为 true

提示

当使用值 trueBrowsableAttribute 构造函数标记属性 (Property) 时,此属性 (Attribute) 的值被设置为常数成员 Yes。对于使用值 falseBrowsableAttribute 构造函数标记的属性 (Property),该值为 No。因此,当检查代码中此属性 (Attribute) 的值时,必须将该属性 (Attribute) 指定为 BrowsableAttribute.YesBrowsableAttribute.No

有关更多信息,请参见 属性 (Attribute) 概述利用属性扩展元数据

示例

下面的示例将属性 (Property) 标记为可浏览。

<Browsable(True)> _
Public Property MyProperty() As Integer
    Get
        ' Insert code here.
        Return 0
    End Get
    Set
        ' Insert code here.
    End Set 
End Property
[Browsable(true)]
 public int MyProperty {
    get {
       // Insert code here.
       return 0;
    }
    set {
       // Insert code here.
    }
 }
public:
   [Browsable(true)]
   property int MyProperty 
   {
      int get()
      {
         // Insert code here.
         return 0;
      }
      void set( int value )
      {
         // Insert code here.
      }
   }
/** @attribute Browsable(true)
 */
/** @property 
 */
public int get_MyProperty()
{
    // Insert code here.
    return 0;
} //get_MyProperty

/** @property 
 */
public void set_MyProperty(int value)
{
    // Insert code here.
} //set_MyProperty
Browsable(true)
public function get MyProperty() : int {
       // Insert code here.
      return 0;
}

public function set MyProperty(value : int) {
}

下一个示例演示如何检查 MyPropertyBrowsableAttribute 的值。首先,代码获取具有该对象的所有属性 (Property) 的 PropertyDescriptorCollection。接着,将代码编入 PropertyDescriptorCollection 的索引,以获取 MyProperty。然后它返回该属性 (Property) 的属性 (Attribute),并将它们保存到属性 (Attribute) 变量中。

该示例提供了两种不同的方法来检查 BrowsableAttribute 的值。在第二个代码段中,该示例调用 Equals 方法。在最后一个代码段中,该示例使用 Browsable 属性 (Property) 检查该值。

' Gets the attributes for the property.
Dim attributes As AttributeCollection = _
    TypeDescriptor.GetProperties(Me)("MyProperty").Attributes

' Checks to see if the value of the BrowsableAttribute is Yes.
If attributes(GetType(BrowsableAttribute)).Equals(BrowsableAttribute.Yes) Then
    ' Insert code here.
End If 

' This is another way to see whether the property is browsable.
Dim myAttribute As BrowsableAttribute = _
    CType(attributes(GetType(BrowsableAttribute)), BrowsableAttribute)
If myAttribute.Browsable Then
    ' Insert code here.
End If 
// Gets the attributes for the property.
 AttributeCollection attributes = 
    TypeDescriptor.GetProperties(this)["MyProperty"].Attributes;
 
 // Checks to see if the value of the BrowsableAttribute is Yes.
 if(attributes[typeof(BrowsableAttribute)].Equals(BrowsableAttribute.Yes)) {
    // Insert code here.
 }
 
 // This is another way to see whether the property is browsable.
 BrowsableAttribute myAttribute = 
    (BrowsableAttribute)attributes[typeof(BrowsableAttribute)];
 if(myAttribute.Browsable) {
    // Insert code here.
 }
// Gets the attributes for the property.
AttributeCollection^ attributes = TypeDescriptor::GetProperties( this )[ "MyProperty" ]->Attributes;

// Checks to see if the value of the BrowsableAttribute is Yes.
if ( attributes[ BrowsableAttribute::typeid ]->Equals( BrowsableAttribute::Yes ) )
{
   
   // Insert code here.
}

// This is another way to see whether the property is browsable.
BrowsableAttribute^ myAttribute = dynamic_cast<BrowsableAttribute^>(attributes[ BrowsableAttribute::typeid ]);
if ( myAttribute->Browsable )
{
   // Insert code here.
}
// Gets the attributes for the property.
AttributeCollection attributes = TypeDescriptor.GetProperties(this).
    get_Item("MyProperty").get_Attributes();
// Checks to see if the value of the BrowsableAttribute is Yes.
if (attributes.get_Item(BrowsableAttribute.class.ToType()).Equals(
    BrowsableAttribute.Yes)) {
    // Insert code here.
}
// This is another way to see whether the property is browsable.
BrowsableAttribute myAttribute = (BrowsableAttribute)
    (attributes.get_Item(BrowsableAttribute.class.ToType()));
if (myAttribute.get_Browsable()) {
    // Insert code here.
}
// Gets the attributes for the property.
 var attributes : AttributeCollection  = 
    TypeDescriptor.GetProperties(this)["MyProperty"].Attributes;
 
 // Checks to see if the value of the BrowsableAttribute is Yes.
 if(attributes[BrowsableAttribute].Equals(BrowsableAttribute.Yes)) {
    Console.WriteLine("MyProperty is browsable.");
 }
 
 // This is another way to see whether the property is browsable.
 var myAttribute : BrowsableAttribute = 
    BrowsableAttribute(attributes[BrowsableAttribute]);
 if(myAttribute.Browsable) {
    Console.WriteLine("MyProperty is browsable.");
 }

如果将类标记为 BrowsableAttribute,请使用下列代码检查该值。

Dim attributes As AttributeCollection = TypeDescriptor.GetAttributes(MyProperty)
If attributes(GetType(BrowsableAttribute)).Equals(BrowsableAttribute.Yes) Then
    ' Insert code here.
End If 
AttributeCollection attributes = 
    TypeDescriptor.GetAttributes(MyProperty);
 if(attributes[typeof(BrowsableAttribute)].Equals(BrowsableAttribute.Yes)) {
    // Insert code here.
 }
AttributeCollection^ attributes = TypeDescriptor::GetAttributes( MyProperty );
if ( attributes[ BrowsableAttribute::typeid ]->Equals( BrowsableAttribute::Yes ) )
{
   // Insert code here.
}
AttributeCollection attributes = 
    TypeDescriptor.GetAttributes((Int32)get_MyProperty());
if (attributes.get_Item(BrowsableAttribute.class.ToType()).Equals(
    BrowsableAttribute.Yes)) {
    // Insert code here.
}
var attributes : AttributeCollection = 
    TypeDescriptor.GetAttributes(MyProperty);
 if(attributes[BrowsableAttribute].Equals(BrowsableAttribute.Yes)) {
    Console.WriteLine("MyProperty is browsable.");
 }

继承层次结构

System.Object
   System.Attribute
    System.ComponentModel.BrowsableAttribute

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

BrowsableAttribute 成员
System.ComponentModel 命名空间
Attribute
PropertyDescriptor
EventDescriptor
AttributeCollection 类
PropertyDescriptorCollection