BrowsableAttribute 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定某一属性或事件是否应在“属性”窗口中显示。
public ref class BrowsableAttribute sealed : Attribute[System.AttributeUsage(System.AttributeTargets.All)]
public sealed class BrowsableAttribute : Attributepublic sealed class BrowsableAttribute : Attribute[<System.AttributeUsage(System.AttributeTargets.All)>]
type BrowsableAttribute = class
    inherit Attributetype BrowsableAttribute = class
    inherit AttributePublic NotInheritable Class BrowsableAttribute
Inherits Attribute- 继承
- 属性
示例
以下示例将属性标记为可浏览。
public:
   [Browsable(true)]
   property int MyProperty 
   {
      int get()
      {
         // Insert code here.
         return 0;
      }
      void set( int value )
      {
         // Insert code here.
      }
   }
[Browsable(true)]
public int MyProperty
{
    get
    {
        // Insert code here.
        return 0;
    }
    set
    {
        // Insert code here.
    }
}
<Browsable(True)> _
Public Property MyProperty() As Integer
    Get
        ' Insert code here.
        Return 0
    End Get
    Set
        ' Insert code here.
    End Set 
End Property
下一个示例演示如何为 检查 的值BrowsableAttributeMyProperty。 首先,代码获取 PropertyDescriptorCollection 包含 对象的所有属性的 。 接下来,将代码索引到 中 PropertyDescriptorCollection 以获取 MyProperty。 然后,它将返回此属性的属性,并将其保存在 attributes 变量中。
该示例演示了检查 的值的 BrowsableAttribute两种不同方法。 第二个代码片段中,该示例调用 Equals 方法。 在最后一个代码片段中,该示例使用 Browsable 属性检查值。
// 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)["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.
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
如果使用 标记类,BrowsableAttribute请使用以下代码检查值。
AttributeCollection^ attributes = TypeDescriptor::GetAttributes( MyProperty );
if ( attributes[ BrowsableAttribute::typeid ]->Equals( BrowsableAttribute::Yes ) )
{
   // Insert code here.
}
AttributeCollection attributes =
    TypeDescriptor.GetAttributes(MyProperty);
if (attributes[typeof(BrowsableAttribute)].Equals(BrowsableAttribute.Yes))
{
    // Insert code here.
}
Dim attributes As AttributeCollection = TypeDescriptor.GetAttributes(MyProperty)
If attributes(GetType(BrowsableAttribute)).Equals(BrowsableAttribute.Yes) Then
    ' Insert code here.
End If
注解
视觉设计器通常显示在 属性窗口那些没有可浏览属性或标记为BrowsableAttribute构造函数browsable参数设置为 true的成员。 可以在设计时修改这些成员。 将 BrowsableAttribute 构造函数 browsable 的参数设置为 false 的成员不适合进行设计时编辑,因此不会显示在可视化设计器中。 默认为 true。
注意
使用 Browsable(true)标记属性时,此属性的值设置为常量成员 Yes。 对于标记为 Browsable(false)的属性,值为 No。 因此,在代码中检查此属性的值时,必须将 特性指定为 BrowsableAttribute.Yes 或 BrowsableAttribute.No。
有关更多信息,请参阅特性。
构造函数
| BrowsableAttribute(Boolean) | 初始化 BrowsableAttribute 类的新实例。 | 
字段
| Default | 指定 BrowsableAttribute 的默认值,即 Yes。 此  | 
| No | 指定一个属性或事件在设计时不能修改。 此  | 
| Yes | 指定一个属性或事件在设计时可以修改。 此  | 
属性
| Browsable | 获取一个值,该值指示此对象是否可浏览。 | 
| TypeId | 在派生类中实现时,获取此 Attribute 的唯一标识符。(继承自 Attribute) | 
方法
| Equals(Object) | 指示此实例与指定对象是否相等。 | 
| GetHashCode() | 返回此实例的哈希代码。 | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| IsDefaultAttribute() | 确定此特性是否为默认特性。 | 
| IsDefaultAttribute() | 在派生类中重写时,指示此实例的值是否是派生类的默认值。(继承自 Attribute) | 
| Match(Object) | 当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。(继承自 Attribute) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) | 
显式接口实现
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) | 将一组名称映射为对应的一组调度标识符。(继承自 Attribute) | 
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) | 检索对象的类型信息,然后可以使用该信息获取接口的类型信息。(继承自 Attribute) | 
| _Attribute.GetTypeInfoCount(UInt32) | 检索对象提供的类型信息接口的数量(0 或 1)。(继承自 Attribute) | 
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) | 提供对某一对象公开的属性和方法的访问。(继承自 Attribute) |