DefaultValueAttribute クラス
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
プロパティの既定値を指定します。
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
- 継承
- 属性
次の例では、 の 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
します。 次に、このプロパティの属性を返し、それらを attributes 変数に保存します。
次に、 から 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 、メンバーが属性の値で自動的に初期化されることはありません。 コードで初期値を設定する必要があります。
詳細については、「属性」を参照してください。
Type |
派生クラスで実装されると、この Attribute の一意の識別子を取得します。 (継承元 Attribute) |
Value |
この属性が関連付けられているプロパティの既定値を取得します。 |
Equals(Object) |
指定したオブジェクトの値が現在の DefaultValueAttribute と等しいかどうかを示す値を返します。 |
Get |
このインスタンスのハッシュ コードを返します。 |
Get |
現在のインスタンスの Type を取得します。 (継承元 Object) |
Is |
派生クラスでオーバーライドされるとき、このインスタンスの値が派生クラスの既定値であるかどうかを示します。 (継承元 Attribute) |
Match(Object) |
派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 (継承元 Attribute) |
Memberwise |
現在の Object の簡易コピーを作成します。 (継承元 Object) |
Set |
この属性が関連付けられているプロパティの既定値を設定します。 |
To |
現在のオブジェクトを表す文字列を返します。 (継承元 Object) |
_Attribute. |
一連の名前を対応する一連のディスパッチ識別子に割り当てます。 (継承元 Attribute) |
_Attribute. |
オブジェクトの型情報を取得します。この情報はインターフェイスの型情報の取得に使用できます。 (継承元 Attribute) |
_Attribute. |
オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。 (継承元 Attribute) |
_Attribute. |
オブジェクトによって公開されたプロパティおよびメソッドへのアクセスを提供します。 (継承元 Attribute) |
製品 | バージョン |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET に関するフィードバック
.NET はオープンソース プロジェクトです。 フィードバックを提供するにはリンクを選択します。