DefaultValueAttribute 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定属性的默认值。
public ref class DefaultValueAttribute : Attribute
public ref class DefaultValueAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.All)]
public class DefaultValueAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.All)]
public sealed class DefaultValueAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.All)>]
type DefaultValueAttribute = class
inherit Attribute
Public Class DefaultValueAttribute
Inherits Attribute
Public NotInheritable Class DefaultValueAttribute
Inherits Attribute
- 继承
- 属性
示例
以下示例将默认值 MyProperty 设置为 false.
private:
bool _myVal;
public:
[DefaultValue(false)]
property bool MyProperty
{
bool get()
{
return _myVal;
}
void set( bool value )
{
_myVal = value;
}
}
[DefaultValue(false)]
public bool MyProperty { get; set; }
Private _myVar As Boolean = False
<DefaultValue(False)>
Public Property MyProperty() As Boolean
Get
Return _myVar
End Get
Set
_myVar = Value
End Set
End Property
下一个示例检查默认值 。MyProperty 首先,代码获取 PropertyDescriptorCollection 对象的所有属性。 接下来,它会索引到 PropertyDescriptorCollection 要获取 MyProperty的 。 然后,它将返回此属性的属性,并将其保存在属性变量中。
然后,该示例通过从AttributeCollection中检索DefaultValueAttribute默认值并将其名称写入控制台屏幕来打印默认值。
// Gets the attributes for the property.
AttributeCollection^ attributes = TypeDescriptor::GetProperties( this )[ "MyProperty" ]->Attributes;
/* Prints the default value by retrieving the DefaultValueAttribute
* from the AttributeCollection. */
DefaultValueAttribute^ myAttribute = dynamic_cast<DefaultValueAttribute^>(attributes[ DefaultValueAttribute::typeid ]);
Console::WriteLine( "The default value is: {0}", myAttribute->Value );
// Gets the attributes for the property.
AttributeCollection attributes =
TypeDescriptor.GetProperties(this)["MyProperty"].Attributes;
/* Prints the default value by retrieving the DefaultValueAttribute
* from the AttributeCollection. */
DefaultValueAttribute myAttribute =
(DefaultValueAttribute)attributes[typeof(DefaultValueAttribute)];
Console.WriteLine("The default value is: " + myAttribute.Value.ToString());
' Gets the attributes for the property.
Dim attributes As AttributeCollection =
TypeDescriptor.GetProperties(Me)("MyProperty").Attributes
' Prints the default value by retrieving the DefaultValueAttribute
' from the AttributeCollection.
Dim myAttribute As DefaultValueAttribute =
CType(attributes(GetType(DefaultValueAttribute)), DefaultValueAttribute)
Console.WriteLine(("The default value is: " & myAttribute.Value.ToString()))
注解
可以使用任何值创建一个 DefaultValueAttribute 。 成员的默认值通常是其初始值。 视觉对象设计器可以使用默认值重置成员的值。 代码生成器还可以使用默认值来确定是否应为成员生成代码。
Note
A DefaultValueAttribute 不会使成员使用特性的值自动初始化。 必须在代码中设置初始值。
有关更多信息,请参阅特性。
构造函数
属性
| 名称 | 说明 |
|---|---|
| TypeId |
在派生类中实现时,获取此 Attribute的唯一标识符。 (继承自 Attribute) |
| Value |
获取此属性绑定到的属性的默认值。 |
方法
| 名称 | 说明 |
|---|---|
| Equals(Object) |
返回给定对象的值是否等于当前 DefaultValueAttribute对象。 |
| GetHashCode() |
返回此实例的哈希代码。 |
| GetType() |
获取当前实例的 Type。 (继承自 Object) |
| IsDefaultAttribute() |
在派生类中重写时,指示此实例的值是否为派生类的默认值。 (继承自 Attribute) |
| Match(Object) |
在派生类中重写时,返回一个值,该值指示此实例是否等于指定对象。 (继承自 Attribute) |
| MemberwiseClone() |
创建当前 Object的浅表副本。 (继承自 Object) |
| SetValue(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) |