[AttributeUsage(AttributeTargets.All)] |
For more information, see the conceptual topic at MSDN: attributesoverview and the conceptual topic at MSDN: extendingmetadatausingattributes.
MyProperty
to false.[DefaultValue(false)] public bool MyProperty { get { // Insert code here. return true; } set { // Insert code here. } }
The next example checks the default value of
MyProperty
. First the code gets a System.ComponentModel.PropertyDescriptorCollection (not supported on the shared source CLI) with all the properties for the object. Next it indexes into the System.ComponentModel.PropertyDescriptorCollection (not supported on the shared source CLI) to get
MyProperty
. Then it returns the attributes for this property and saves them in the attributes variable.
The example then prints the default value by retrieving the DefaultValueAttribute from the System.ComponentModel.AttributeCollection (not supported on the shared source CLI) , and writing its name to the console screen.
// 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());
ctor #1 | Overloaded:.ctor(bool value) Initializes a new instance of the DefaultValueAttribute class using a Boolean value. |
ctor #2 | Overloaded:.ctor(byte value) Initializes a new instance of the DefaultValueAttribute class using an 8-bit unsigned integer. |
ctor #3 | Overloaded:.ctor(char value) Initializes a new instance of the DefaultValueAttribute class using a Unicode character. |
ctor #4 | Overloaded:.ctor(double value) Initializes a new instance of the DefaultValueAttribute class using a double-precision floating point number. |
ctor #5 | Overloaded:.ctor(short value) Initializes a new instance of the DefaultValueAttribute class using a 16-bit signed integer. |
ctor #6 | Overloaded:.ctor(int value) Initializes a new instance of the DefaultValueAttribute class using a 32-bit signed integer. |
ctor #7 | Overloaded:.ctor(long value) Initializes a new instance of the DefaultValueAttribute class using a 64-bit signed integer. |
ctor #8 | Overloaded:.ctor(object value) Initializes a new instance of the DefaultValueAttribute class. |
ctor #9 | Overloaded:.ctor(float value) Initializes a new instance of the DefaultValueAttribute class using a single-precision floating point number. |
ctor #10 | Overloaded:.ctor(string value) Initializes a new instance of the DefaultValueAttribute class using a String. |
TypeId (inherited from System.Attribute) |
Read-only See base class member description: System.Attribute.TypeId When implemented in a derived class, gets a unique identifier for this Attribute. |
Value | Read-only Gets the default value of the property this attribute is bound to. |
Equals | Overridden: Returns whether the value of the given object is equal to the current DefaultValueAttribute. |
GetHashCode | Overridden: Supports the Shared Source CLI infrastructure and is not intended to be used directly from your code |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
IsDefaultAttribute (inherited from System.Attribute) |
See base class member description: System.Attribute.IsDefaultAttribute When overridden in a derived class, returns an indication whether the value of this instance is the default value for the derived class. |
Match (inherited from System.Attribute) |
See base class member description: System.Attribute.Match When overridden in a derived class, returns a value indicating whether this instance equals a specified object. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public DefaultValueAttribute( |
value
public DefaultValueAttribute( |
value
public DefaultValueAttribute( |
value
public DefaultValueAttribute( |
value
public DefaultValueAttribute( |
value
public DefaultValueAttribute( |
value
public DefaultValueAttribute( |
value
public DefaultValueAttribute( |
value
public DefaultValueAttribute( |
value
public DefaultValueAttribute( |
value
public virtual object TypeId {get;}
|
public object Value {get;}
|
obj
~DefaultValueAttribute(); |
public override int GetHashCode(); |
public Type GetType(); |
public virtual bool IsDefaultAttribute(); |
The implementation of this method in a derived class compares the value of this instance to a standard, default value obtained by some means, then returns a Boolean value that indicates whether the value of this instance is equal to the standard. The standard value is typically coded as a constant in the implementation, or stored programmatically in a field used by the implementation.
obj
protected object MemberwiseClone(); |
public virtual string ToString(); |