BrowsableAttribute 类
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定某一属性或事件是否应在“属性”窗口中显示。
public ref class BrowsableAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.All)]
public sealed class BrowsableAttribute : Attribute
public sealed class BrowsableAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.All)>]
type BrowsableAttribute = class
inherit Attribute
type BrowsableAttribute = class
inherit Attribute
Public 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。
有关更多信息,请参阅特性。
Browsable |
初始化 BrowsableAttribute 类的新实例。 |
Default |
指定 BrowsableAttribute 的默认值,即 Yes。 此 |
No |
指定一个属性或事件在设计时不能修改。 此 |
Yes |
指定一个属性或事件在设计时可以修改。 此 |
Equals(Object) |
指示此实例与指定对象是否相等。 |
Get |
返回此实例的哈希代码。 |
Get |
获取当前实例的 Type。 (继承自 Object) |
Is |
确定此特性是否为默认特性。 |
Is |
在派生类中重写时,指示此实例的值是否是派生类的默认值。 (继承自 Attribute) |
Match(Object) |
当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。 (继承自 Attribute) |
Memberwise |
创建当前 Object 的浅表副本。 (继承自 Object) |
To |
返回表示当前对象的字符串。 (继承自 Object) |
_Attribute. |
将一组名称映射为对应的一组调度标识符。 (继承自 Attribute) |
_Attribute. |
检索对象的类型信息,然后可以使用该信息获取接口的类型信息。 (继承自 Attribute) |
_Attribute. |
检索对象提供的类型信息接口的数量(0 或 1)。 (继承自 Attribute) |
_Attribute. |
提供对某一对象公开的属性和方法的访问。 (继承自 Attribute) |
产品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |