DefaultValueAttribute クラス
アセンブリ: System (system.dll 内)
構文
<AttributeUsageAttribute(AttributeTargets.All)> _ Public Class DefaultValueAttribute Inherits Attribute
[AttributeUsageAttribute(AttributeTargets::All)] public ref class DefaultValueAttribute : public Attribute
任意の値を指定して DefaultValueAttribute を作成できます。通常、メンバの既定値はその初期値になります。ビジュアル デザイナでは、その既定値を使用してメンバの値をリセットできます。コード ジェネレータでも、既定値を使用して、メンバに対してコードを生成する必要があるかどうかを判断できます。
詳細については、属性の概要、属性を使用したメタデータの拡張 の各トピックを参照してください。
MyProperty の既定値として false を設定する例を次に示します。
Private MyVar as Boolean = False <DefaultValue(False)> _ Public Property MyProperty() As Boolean Get Return MyVar End Get Set MyVar = Value End Set End Property
private bool myVal=false; [DefaultValue(false)] public bool MyProperty { get { return myVal; } set { myVal=value; } }
private: bool myVal; public: [DefaultValue(false)] property bool MyProperty { bool get() { return myVal; } void set( bool value ) { myVal = value; } }
private boolean myVal = false; /** @attribute DefaultValue(false) */ /** @property */ public boolean get_MyProperty() { return myVal; } //get_MyProperty /** @property */ public void set_MyProperty(boolean value) { myVal = value; } //set_MyProperty
MyProperty の既定値を確認する例を次に示します。最初に、オブジェクトのすべてのプロパティを保持する PropertyDescriptorCollection を取得します。次に、インデックスを付けて PropertyDescriptorCollection から MyProperty を取得します。そして、このプロパティの属性を返し、その属性を属性変数に保存します。
最後に、AttributeCollection から DefaultValueAttribute を取得し、その名前をコンソール画面に書き込むことによって、既定値を出力します。
' 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()))
// 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. 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). get_Item("MyProperty").get_Attributes(); /* Prints the default value by retrieving the DefaultValueAttribute from the AttributeCollection. */ DefaultValueAttribute myAttribute = (DefaultValueAttribute)(attributes. get_Item(DefaultValueAttribute.class.ToType())); Console.WriteLine(("The default value is: " + myAttribute.get_Value().ToString()));
System.Attribute
System.ComponentModel.DefaultValueAttribute
プラットフォーム
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- DefaultValueAttribute クラスのページへのリンク