BrowsableAttribute 类

定义

指定某一属性或事件是否应在“属性”窗口中显示。Specifies whether a property or event should be displayed in a Properties window.

public ref class BrowsableAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.All)]
public sealed class BrowsableAttribute : Attribute
type BrowsableAttribute = class
    inherit Attribute
Public NotInheritable Class BrowsableAttribute
Inherits Attribute
继承
BrowsableAttribute
属性

示例

下面的示例将属性标记为可浏览。The following example marks a property as browsable.

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

下一个示例演示如何检查的值BrowsableAttributeMyPropertyThe next example shows how to check the value of the BrowsableAttribute for MyProperty. 首先, 该代码获取一个PropertyDescriptorCollection , 其中包含对象的所有属性。First, the code gets a PropertyDescriptorCollection with all the properties for the object. 接下来, 代码将索引PropertyDescriptorCollection到中以获取。 MyPropertyNext, the code indexes into the PropertyDescriptorCollection to get MyProperty. 然后, 它将返回此属性的特性并将其保存在特性变量中。Then it returns the attributes for this property and saves them in the attributes variable.

该示例提供了两种不同的方法来检查的BrowsableAttribute值。The example presents two different ways of checking the value of the BrowsableAttribute. 在第二个代码片段中, 该示例Equals调用方法。In the second code fragment, the example calls the Equals method. 在上一个代码段中, 该示例使用Browsable属性来检查值。In the last code fragment, the example uses the Browsable property to check the value.

// 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标记了类, 请使用以下代码来检查值。If you marked a class with the BrowsableAttribute, use the following code to check the value.

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 

注解

可视化设计器通常显示在属性窗口那些没有可浏览属性或标记BrowsableAttributebrowsable构造函数参数设置为true的成员。A visual designer typically displays in the Properties window those members that either have no browsable attribute or are marked with the BrowsableAttribute constructor's browsable parameter set to true. 这些成员可以在设计时修改。These members can be modified at design time. 标记BrowsableAttribute为构造函数的browsable参数设置为false的成员不适合设计时编辑, 因此不会显示在可视化设计器中。Members marked with the BrowsableAttribute constructor's browsable parameter set to false are not appropriate for design-time editing and therefore are not displayed in a visual designer. 默认值为 trueThe default is true.

备注

当使用Browsable(true)标记属性时, 此属性的值将设置为常量成员YesWhen you mark a property with Browsable(true), the value of this attribute is set to the constant member Yes. 对于标记Browsable(false)为的属性, 值为NoFor a property marked with Browsable(false), the value is No. 因此, 当您在代码中检查此属性的值时, 必须将属性指定为BrowsableAttribute.Yes或。 BrowsableAttribute.NoTherefore, when you check the value of this attribute in your code, you must specify the attribute as BrowsableAttribute.Yes or BrowsableAttribute.No.

有关更多信息,请参阅特性For more information, see Attributes.

构造函数

BrowsableAttribute(Boolean)

初始化 BrowsableAttribute 类的新实例。Initializes a new instance of the BrowsableAttribute class.

字段

Default

指定 BrowsableAttribute 的默认值,即 YesSpecifies the default value for the BrowsableAttribute, which is Yes. static 字段是只读的。This static field is read-only.

No

指定一个属性或事件在设计时不能修改。Specifies that a property or event cannot be modified at design time. static 字段是只读的。This static field is read-only.

Yes

指定一个属性或事件在设计时可以修改。Specifies that a property or event can be modified at design time. static 字段是只读的。This static field is read-only.

属性

Browsable

获取一个值,该值指示此对象是否可浏览。Gets a value indicating whether an object is browsable.

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(继承自 Attribute)

方法

Equals(Object)

指示此实例与指定对象是否相等。Indicates whether this instance and a specified object are equal.

GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
IsDefaultAttribute()

确定此特性是否为默认特性。Determines if this attribute is the default.

Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(继承自 Attribute)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

显式界面实现

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(继承自 Attribute)

适用于

另请参阅