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 作成できます。 通常、メンバーの既定値は初期値です。 ビジュアル デザイナーでは、既定値を使用してメンバーの値をリセットできます。 コード ジェネレーターは、既定値を使用して、メンバーに対してコードを生成する必要があるかどうかを判断することもできます。

Note

DefaultValueAttributeでは、属性の値を使用してメンバーが自動的に初期化されることはありません。 コードで初期値を設定する必要があります。

詳細については、「属性」を参照してください。

コンストラクター

DefaultValueAttribute(Boolean)

DefaultValueAttribute クラスの新しいインスタンスを Boolean 値に初期化します。

DefaultValueAttribute(Byte)

DefaultValueAttribute クラスの新しいインスタンスを 8 ビット符号なし整数に初期化します。

DefaultValueAttribute(Char)

DefaultValueAttribute クラスの新しいインスタンスを Unicode 文字に初期化します。

DefaultValueAttribute(Double)

DefaultValueAttribute クラスの新しいインスタンスを倍精度浮動小数点数に初期化します。

DefaultValueAttribute(Int16)

DefaultValueAttribute クラスの新しいインスタンスを 16 ビット符号付き整数に初期化します。

DefaultValueAttribute(Int32)

DefaultValueAttribute クラスの新しいインスタンスを 32 ビット符号付き整数に初期化します。

DefaultValueAttribute(Int64)

DefaultValueAttribute クラスの新しいインスタンスを 64 ビット符号付き整数に初期化します。

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

この属性が関連付けられているプロパティの既定値を取得します。

メソッド

Equals(Object)

指定したオブジェクトの値が現在の DefaultValueAttribute と等しいかどうかを示す値を返します。

GetHashCode()

このインスタンスのハッシュ コードを返します。

GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
IsDefaultAttribute()

派生クラスでオーバーライドされるとき、このインスタンスの値が派生クラスの既定値であるかどうかを示します。

(継承元 Attribute)
Match(Object)

派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。

(継承元 Attribute)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
SetValue(Object)

この属性が関連付けられているプロパティの既定値を設定します。

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)

適用対象

こちらもご覧ください