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の既定値を確認します。 まず、コードはオブジェクトのすべてのプロパティを含む 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)

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)

Stringを使用して、DefaultValueAttribute クラスの新しいインスタンスを初期化します。

DefaultValueAttribute(Type, String)

DefaultValueAttribute クラスの新しいインスタンスを初期化し、指定した値を指定した型に変換し、インバリアント カルチャを変換コンテキストとして使用します。

DefaultValueAttribute(UInt16)

UInt16値を使用して、DefaultValueAttribute クラスの新しいインスタンスを初期化します。

DefaultValueAttribute(UInt32)

UInt32値を使用して、DefaultValueAttribute クラスの新しいインスタンスを初期化します。

DefaultValueAttribute(UInt64)

UInt64値を使用して、DefaultValueAttribute クラスの新しいインスタンスを初期化します。

プロパティ

名前 説明
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)

適用対象

こちらもご覧ください