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 。 成員的預設值通常是其初始值。 視覺化設計工具可以使用預設值來重設成員的值。 程式碼產生器也可以使用預設值來判斷是否應該為成員產生程式碼。

注意

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)

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

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱