DefaultValueAttribute DefaultValueAttribute DefaultValueAttribute DefaultValueAttribute Class

定義

指定屬性的預設值。Specifies the default value for a property.

public ref class DefaultValueAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.All)]
public class DefaultValueAttribute : Attribute
type DefaultValueAttribute = class
    inherit Attribute
Public Class DefaultValueAttribute
Inherits Attribute
繼承
DefaultValueAttributeDefaultValueAttributeDefaultValueAttributeDefaultValueAttribute
屬性

範例

下列範例會設定的預設值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.

然後藉由擷取列印預設值的範例DefaultValueAttributeAttributeCollection,和其名稱寫入至主控台畫面。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.

注意

ADefaultValueAttribute不會導致成員,才能使用該屬性的值時,會自動初始化。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) DefaultValueAttribute(Boolean) DefaultValueAttribute(Boolean)

使用 DefaultValueAttribute 值來初始化 Boolean 類別的新執行個體。Initializes a new instance of the DefaultValueAttribute class using a Boolean value.

DefaultValueAttribute(Byte) DefaultValueAttribute(Byte) DefaultValueAttribute(Byte) DefaultValueAttribute(Byte)

使用 8 位元不帶正負號的整數來初始化 DefaultValueAttribute 類別的新執行個體。Initializes a new instance of the DefaultValueAttribute class using an 8-bit unsigned integer.

DefaultValueAttribute(Char) DefaultValueAttribute(Char) DefaultValueAttribute(Char) DefaultValueAttribute(Char)

使用 Unicode 字元來初始化 DefaultValueAttribute 類別的新執行個體。Initializes a new instance of the DefaultValueAttribute class using a Unicode character.

DefaultValueAttribute(Double) DefaultValueAttribute(Double) DefaultValueAttribute(Double) DefaultValueAttribute(Double)

使用雙精確度浮點數來初始化 DefaultValueAttribute 類別的新執行個體。Initializes a new instance of the DefaultValueAttribute class using a double-precision floating point number.

DefaultValueAttribute(Int16) DefaultValueAttribute(Int16) DefaultValueAttribute(Int16) DefaultValueAttribute(Int16)

使用 16 位元帶正負號的整數來初始化 DefaultValueAttribute 類別的新執行個體。Initializes a new instance of the DefaultValueAttribute class using a 16-bit signed integer.

DefaultValueAttribute(Int32) DefaultValueAttribute(Int32) DefaultValueAttribute(Int32) DefaultValueAttribute(Int32)

使用 32 位元帶正負號的整數來初始化 DefaultValueAttribute 類別的新執行個體。Initializes a new instance of the DefaultValueAttribute class using a 32-bit signed integer.

DefaultValueAttribute(Int64) DefaultValueAttribute(Int64) DefaultValueAttribute(Int64) DefaultValueAttribute(Int64)

使用 64 位元帶正負號的整數來初始化 DefaultValueAttribute 類別的新執行個體。Initializes a new instance of the DefaultValueAttribute class using a 64-bit signed integer.

DefaultValueAttribute(Object) DefaultValueAttribute(Object) DefaultValueAttribute(Object) DefaultValueAttribute(Object)

初始化 DefaultValueAttribute 類別的新執行個體。Initializes a new instance of the DefaultValueAttribute class.

DefaultValueAttribute(SByte) DefaultValueAttribute(SByte) DefaultValueAttribute(SByte) DefaultValueAttribute(SByte)
DefaultValueAttribute(Single) DefaultValueAttribute(Single) DefaultValueAttribute(Single) DefaultValueAttribute(Single)

使用單精確度浮點數來初始化 DefaultValueAttribute 類別的新執行個體。Initializes a new instance of the DefaultValueAttribute class using a single-precision floating point number.

DefaultValueAttribute(String) DefaultValueAttribute(String) DefaultValueAttribute(String) DefaultValueAttribute(String)

使用 DefaultValueAttribute 來初始化 String 類別的新執行個體。Initializes a new instance of the DefaultValueAttribute class using a String.

DefaultValueAttribute(Type, String) DefaultValueAttribute(Type, String) DefaultValueAttribute(Type, String) DefaultValueAttribute(Type, String)

將指定的值轉換為指定型別,並使用不變文化特性 (Invariant Culture) 做為轉譯內容,初始化 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) DefaultValueAttribute(UInt16) DefaultValueAttribute(UInt16)
DefaultValueAttribute(UInt32) DefaultValueAttribute(UInt32) DefaultValueAttribute(UInt32) DefaultValueAttribute(UInt32)
DefaultValueAttribute(UInt64) DefaultValueAttribute(UInt64) DefaultValueAttribute(UInt64) DefaultValueAttribute(UInt64)

屬性

TypeId TypeId TypeId TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)
Value Value Value Value

取得這個屬性 (Attribute) 所繫結之屬性 (Property) 的預設值。Gets the default value of the property this attribute is bound to.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

傳回值,表示指定的物件值是否等於目前的 DefaultValueAttributeReturns whether the value of the given object is equal to the current DefaultValueAttribute.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

傳回這個執行個體的雜湊碼。Returns the hash code for this instance.

GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(Inherited from Attribute)
Match(Object) Match(Object) Match(Object) Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(Inherited from Object)
SetValue(Object) SetValue(Object) SetValue(Object) SetValue(Object)

設定這個屬性 (Attribute) 所繫結之屬性 (Property) 的預設值。Sets the default value for the property to which this attribute is bound.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

明確介面實作

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

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。Provides access to properties and methods exposed by an object.

(Inherited from Attribute)

適用於

另請參閱