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;
      }
   }
[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預設值。 首先,程式碼會取得包含物件所有屬性的 a PropertyDescriptorCollection 。 接著它會索引到 PropertyDescriptorCollection ,取得 MyProperty。 接著它會回傳這個屬性的屬性,並儲存在屬性變數中。

範例接著從 中取得 ,DefaultValueAttributeAttributeCollection並將其名稱寫入主控台畫面,以列印預設值。

// 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()))

備註

你可以創造任何數值的 a DefaultValueAttribute 。 成員的預設值通常是其初始值。 視覺設計師可以使用預設值來重置成員的值。 程式碼產生器也可以利用預設值來判斷是否應該為該成員產生程式碼。

Note

A DefaultValueAttribute 不會讓成員自動以該屬性的值初始化。 你必須在程式碼中設定初始值。

如需詳細資訊,請參閱屬性

建構函式

名稱 Description
DefaultValueAttribute(Boolean)

使用值Boolean初始化該類別的新實例DefaultValueAttribute

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)

使用值SByte初始化該類別的新實例DefaultValueAttribute

DefaultValueAttribute(Single)

使用單精度浮點數初始化該類別的新實例 DefaultValueAttribute

DefaultValueAttribute(String)

使用 DefaultValueAttributeString

DefaultValueAttribute(Type, String)

初始化類別的新實例 DefaultValueAttribute ,將指定值轉換為指定型別,並使用不變文化作為翻譯上下文。

DefaultValueAttribute(UInt16)

使用值UInt16初始化該類別的新實例DefaultValueAttribute

DefaultValueAttribute(UInt32)

使用值UInt32初始化該類別的新實例DefaultValueAttribute

DefaultValueAttribute(UInt64)

使用值UInt64初始化該類別的新實例DefaultValueAttribute

屬性

名稱 Description
TypeId

在衍生類別中實作時,取得這個 Attribute的唯一標識碼。

(繼承來源 Attribute)
Value

取得該屬性綁定的屬性的預設值。

方法

名稱 Description
Equals(Object)

回傳給定物件的值是否等於當前 DefaultValueAttribute值。

GetHashCode()

傳回這個實例的哈希碼。

GetType()

取得目前實例的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,指出這個實例的值是否為衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,傳回值,指出這個實例是否等於指定的物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
SetValue(Object)

設定該屬性所綁定屬性的預設值。

ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

明確介面實作

名稱 Description
_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)

適用於

另請參閱