Assembly: System (in system.dll)
<AttributeUsageAttribute(AttributeTargets.All)> _ Public Class DefaultValueAttribute Inherits Attribute
Dim instance As DefaultValueAttribute
[AttributeUsageAttribute(AttributeTargets.All)] public class DefaultValueAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::All)] public ref class DefaultValueAttribute : public Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.All) */ public class DefaultValueAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.All) public class DefaultValueAttribute extends Attribute
È possibile creare un oggetto DefaultValueAttribute utilizzando qualsiasi valore. Il valore predefinito di un membro corrisponde in genere al valore iniziale. In una finestra di progettazione visiva il valore predefinito può essere utilizzato per reimpostare il valore del membro, mentre i generatori di codice utilizzano i valori predefiniti anche per determinare se per il membro deve essere generato del codice.
Per ulteriori informazioni, vedere Cenni preliminari sugli attributi e Estensione di metadati mediante attributi.
Nell'esempio riportato di seguito il valore predefinito di MyProperty viene impostato su 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
Nell'esempio successivo si controlla il valore predefinito di MyProperty. Viene ottenuto dapprima un insieme PropertyDescriptorCollection con tutte le proprietà dell'oggetto, quindi viene effettuata l'indicizzazione in PropertyDescriptorCollection per ottenere MyProperty. Vengono infine restituiti gli attributi per questa proprietà, che vengono salvati nella variabile degli attributi.
Viene quindi visualizzato il valore predefinito recuperando l'oggetto DefaultValueAttribute da AttributeCollection e inserendone il nome nella finestra della console.
' 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 per Pocket PC, Windows Mobile per Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework non supporta tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema.