DefaultValueAttribute 类

定义

指定属性的默认值。

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
继承
DefaultValueAttribute
属性

示例

以下示例将默认值 MyProperty 设置为 false.

private:
   bool _myVal;

public:
   [DefaultValue(false)]
   property bool MyProperty 
   {
      bool get()
      {
         return _myVal;
      }

      void set( bool value )
      {
         _myVal = value;
      }
   }

private bool _myVal = false;

[DefaultValue(false)]
public bool MyProperty
{
    get
    {
        return _myVal;
    }
    set
    {
        _myVal = value;
    }
}

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 。 成员的默认值通常是其初始值。 可视化设计器可以使用默认值重置成员的值。 代码生成器还可以使用默认值来确定是否应为成员生成代码。

备注

A DefaultValueAttribute 不会使成员使用特性的值自动初始化。 必须在代码中设置初始值。

有关更多信息,请参阅特性

构造函数

DefaultValueAttribute(Boolean)

使用 DefaultValueAttribute 值初始化 Boolean 类的新实例。

DefaultValueAttribute(Byte)

使用 8 位无符号整数初始化 DefaultValueAttribute 类的新实例。

DefaultValueAttribute(Char)

使用 Unicode 字符初始化 DefaultValueAttribute 类的新实例。

DefaultValueAttribute(Double)

使用双精度浮点数字初始化 DefaultValueAttribute 类的新实例。

DefaultValueAttribute(Int16)

使用 16 位有符号整数初始化 DefaultValueAttribute 类的实例。

DefaultValueAttribute(Int32)

使用 32 位有符号整数初始化 DefaultValueAttribute 类的新实例。

DefaultValueAttribute(Int64)

使用 64 位有符号整数初始化 DefaultValueAttribute 类的新实例。

DefaultValueAttribute(Object)

初始化 DefaultValueAttribute 类的新实例。

DefaultValueAttribute(SByte)

使用 DefaultValueAttribute 值初始化 SByte 类的新实例。

DefaultValueAttribute(Single)

使用单精度浮点数字初始化 DefaultValueAttribute 类的新实例。

DefaultValueAttribute(String)

使用 DefaultValueAttribute 初始化 String 类的新实例。

DefaultValueAttribute(Type, String)

初始化 DefaultValueAttribute 类的新实例,将指定的值转换为指定的类型,并将固定区域性作为翻译上下文。

DefaultValueAttribute(UInt16)

使用 DefaultValueAttribute 值初始化 UInt16 类的新实例。

DefaultValueAttribute(UInt32)

使用 DefaultValueAttribute 值初始化 UInt32 类的新实例。

DefaultValueAttribute(UInt64)

使用 DefaultValueAttribute 值初始化 UInt64 类的新实例。

属性

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。

(继承自 Attribute)
Value

获取此属性 (Attribute) 绑定到的属性 (Property) 的默认值。

方法

Equals(Object)

返回给定对象的值是否等于当前的 DefaultValueAttribute

GetHashCode()

返回此实例的哈希代码。

GetType()

获取当前实例的 Type

(继承自 Object)
IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。

(继承自 Attribute)
Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。

(继承自 Attribute)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
SetValue(Object)

设置此属性 (Attribute) 所绑定到的属性 (Property) 的默认值。

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)

适用于

另请参阅