DefaultValueAttribute 类

指定属性 (Property) 的默认值。

**命名空间:**System.ComponentModel
**程序集:**System(在 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

备注

可以使用任何值创建 DefaultValueAttribute。成员的默认值通常是其初始值。可视化设计器可以使用默认值重置成员的值。代码生成器也可使用默认值确定是否为成员生成代码。

有关更多信息,请参见 属性 (Attribute) 概述利用属性扩展元数据

主题 位置
演练:开发和使用自定义服务器控件 创作 ASP.NET 控件
为 ASP.NET 1.1 开发自定义数据绑定 Web 服务器控件 创作 ASP.NET 控件
为 ASP.NET 2.0 开发自定义数据绑定 Web 服务器控件 创作 ASP.NET 控件
演练:为 ASP.NET 2.0 创建自定义数据绑定 ASP.NET Web 控件 创作 ASP.NET 控件
演练:为 ASP.NET 1.1 创建自定义数据绑定 ASP.NET Web 控件 创作 ASP.NET 控件
演练:开发和使用自定义服务器控件 使用 Visual Web Developer 生成应用程序
为 ASP.NET 2.0 开发自定义数据绑定 Web 服务器控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:为 ASP.NET 2.0 创建自定义数据绑定 ASP.NET Web 控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
为 ASP.NET 1.1 开发自定义数据绑定 Web 服务器控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
为 ASP.NET 2.0 开发自定义数据绑定 Web 服务器控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:为 ASP.NET 2.0 创建自定义数据绑定 ASP.NET Web 控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:为 ASP.NET 1.1 创建自定义数据绑定 ASP.NET Web 控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
为 ASP.NET 1.1 开发自定义数据绑定 Web 服务器控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:为 ASP.NET 1.1 创建自定义数据绑定 ASP.NET Web 控件 在 Visual Studio 中生成 ASP .NET Web 应用程序

示例

下面的示例将 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 的默认值。首先,代码获取具有该对象的所有属性 (Property) 的 PropertyDescriptorCollection。接下来,对 PropertyDescriptorCollection 进行索引,以获取 MyProperty。然后它返回该属性 (Property) 的属性 (Attribute),并将它们保存到属性 (Attribute) 变量中。

接着该示例通过从 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.Object
   System.Attribute
    System.ComponentModel.DefaultValueAttribute

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

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 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

DefaultValueAttribute 成员
System.ComponentModel 命名空间
Attribute
PropertyDescriptor