DefaultValueAttribute 类  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定属性的默认值。
public ref class DefaultValueAttribute : Attributepublic 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 AttributePublic Class DefaultValueAttribute
Inherits AttributePublic 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不会导致成员使用 特性的值自动初始化。 必须在代码中设置初始值。
有关更多信息,请参阅特性。
构造函数
属性
| TypeId | 在派生类中实现时,获取此 Attribute 的唯一标识符。(继承自 Attribute) | 
| Value | 获取此属性 (Attribute) 绑定到的属性 (Property) 的默认值。 | 
方法
| Equals(Object) | 返回给定对象的值是否等于当前的 DefaultValueAttribute。 | 
| GetHashCode() | 返回此实例的哈希代码。 | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| IsDefaultAttribute() | 在派生类中重写时,指示此实例的值是否是派生类的默认值。(继承自 Attribute) | 
| Match(Object) | 当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。(继承自 Attribute) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| SetValue(Object) | 设置此属性 (Attribute) 所绑定到的属性 (Property) 的默认值。 | 
| 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) |