DefaultValueAttribute 类

定义

指定属性的默认值。Specifies the default value for a property.

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

示例

下面的示例将的默认值设置 MyPropertyfalseThe following example sets the default value of MyProperty to 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

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

然后,该示例通过 DefaultValueAttribute 从检索 AttributeCollection ,并将其名称写入控制台屏幕来打印默认值。The example then prints the default value by retrieving the DefaultValueAttribute from the AttributeCollection, and writing its name to the console screen.

// 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 使用任何值创建。You can create a DefaultValueAttribute with any value. 成员的默认值通常为其初始值。A member's default value is typically its initial value. 可视化设计器可以使用默认值来重置成员的值。A visual designer can use the default value to reset the member's value. 代码生成器还可以使用默认值来确定是否应为成员生成代码。Code generators can use the default values also to determine whether code should be generated for the member.

备注

DefaultValueAttribute不会导致成员使用特性的值自动初始化。A DefaultValueAttribute will not cause a member to be automatically initialized with the attribute's value. 你必须在代码中设置初始值。You must set the initial value in your code.

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

构造函数

DefaultValueAttribute(Boolean)

使用 DefaultValueAttribute 值初始化 Boolean 类的新实例。Initializes a new instance of the DefaultValueAttribute class using a Boolean value.

DefaultValueAttribute(Byte)

使用 8 位无符号整数初始化 DefaultValueAttribute 类的新实例。Initializes a new instance of the DefaultValueAttribute class using an 8-bit unsigned integer.

DefaultValueAttribute(Char)

使用 Unicode 字符初始化 DefaultValueAttribute 类的新实例。Initializes a new instance of the DefaultValueAttribute class using a Unicode character.

DefaultValueAttribute(Double)

使用双精度浮点数字初始化 DefaultValueAttribute 类的新实例。Initializes a new instance of the DefaultValueAttribute class using a double-precision floating point number.

DefaultValueAttribute(Int16)

使用 16 位有符号整数初始化 DefaultValueAttribute 类的实例。Initializes a new instance of the DefaultValueAttribute class using a 16-bit signed integer.

DefaultValueAttribute(Int32)

使用 32 位有符号整数初始化 DefaultValueAttribute 类的新实例。Initializes a new instance of the DefaultValueAttribute class using a 32-bit signed integer.

DefaultValueAttribute(Int64)

使用 64 位有符号整数初始化 DefaultValueAttribute 类的新实例。Initializes a new instance of the DefaultValueAttribute class using a 64-bit signed integer.

DefaultValueAttribute(Object)

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

DefaultValueAttribute(SByte)

使用 DefaultValueAttribute 值初始化 SByte 类的新实例。Initializes a new instance of the DefaultValueAttribute class using a SByte value.

DefaultValueAttribute(Single)

使用单精度浮点数字初始化 DefaultValueAttribute 类的新实例。Initializes a new instance of the DefaultValueAttribute class using a single-precision floating point number.

DefaultValueAttribute(String)

使用 DefaultValueAttribute 初始化 String 类的新实例。Initializes a new instance of the DefaultValueAttribute class using a String.

DefaultValueAttribute(Type, String)

初始化 DefaultValueAttribute 类的新实例,将指定的值转换为指定的类型,并将固定区域性作为翻译上下文。Initializes a new instance of the DefaultValueAttribute class, converting the specified value to the specified type, and using an invariant culture as the translation context.

DefaultValueAttribute(UInt16)

使用 DefaultValueAttribute 值初始化 UInt16 类的新实例。Initializes a new instance of the DefaultValueAttribute class using a UInt16 value.

DefaultValueAttribute(UInt32)

使用 DefaultValueAttribute 值初始化 UInt32 类的新实例。Initializes a new instance of the DefaultValueAttribute class using a UInt32 value.

DefaultValueAttribute(UInt64)

使用 DefaultValueAttribute 值初始化 UInt64 类的新实例。Initializes a new instance of the DefaultValueAttribute class using a UInt64 value.

属性

TypeId

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

(继承自 Attribute)
Value

获取此属性 (Attribute) 绑定到的属性 (Property) 的默认值。Gets the default value of the property this attribute is bound to.

方法

Equals(Object)

返回给定对象的值是否等于当前的 DefaultValueAttributeReturns whether the value of the given object is equal to the current DefaultValueAttribute.

GetHashCode()

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

GetType()

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

(继承自 Object)
IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(继承自 Attribute)
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)
SetValue(Object)

设置此属性 (Attribute) 所绑定到的属性 (Property) 的默认值。Sets the default value for the property to which this attribute is bound.

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)

适用于

另请参阅