DescriptionAttribute 类

指定属性 (Property) 或事件的说明。

**命名空间:**System.ComponentModel
**程序集:**System(在 system.dll 中)

语法

声明
<AttributeUsageAttribute(AttributeTargets.All)> _
Public Class DescriptionAttribute
    Inherits Attribute
用法
Dim instance As DescriptionAttribute
[AttributeUsageAttribute(AttributeTargets.All)] 
public class DescriptionAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::All)] 
public ref class DescriptionAttribute : public Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.All) */ 
public class DescriptionAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.All) 
public class DescriptionAttribute extends Attribute

备注

可视化设计器在引用组件成员时可以显示指定的说明,如在“属性”窗口中。调用 Description 访问该属性 (Attribute) 的值。

有关更多信息,请参见 属性 (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 应用程序

示例

下面的示例创建 MyImage 属性 (Property)。该属性 (Property) 有两个属性 (Attribute):DescriptionAttributeCategoryAttribute

<Description("The image associated with the control"), _
    Category("Appearance")> _
Public Property MyImage() As Image
    Get
        ' Insert code here.
        Return image1
    End Get
    Set
        ' Insert code here.
    End Set 
End Property
[Description("The image associated with the control"),Category("Appearance")] 
 public Image MyImage {
    get {
       // Insert code here.
       return image1;
    }
    set {
       // Insert code here.
    }
 }
public:
   property Image^ MyImage 
   {
      [Description("The image associated with the control"),Category("Appearance")]
      Image^ get()
      {
         // Insert code here.
         return image1;
      }

      void set( Image^ value )
      {
         // Insert code here.
      }
   }
/** @attribute Description("The image associated with the control")
    @attribute Category("Appearance")
 */
/** @property 
 */
public Image get_MyImage()
{
    // Insert code here.
    return image1;
} //get_MyImage

/** @property 
 */
public void set_MyImage(Image value)
{
    // Insert code here.
} //set_MyImage

下一个示例获取 MyImage 的说明。首先,代码获取具有该对象的所有属性 (Property) 的 PropertyDescriptorCollection。接下来,对 PropertyDescriptorCollection 进行索引,以获取 MyImage。然后它返回该属性 (Property) 的属性 (Attribute),并将它们保存到属性 (Attribute) 变量中。

然后该示例通过以下方法输出说明:从 AttributeCollection 检索 DescriptionAttribute,然后将它写到控制台屏幕上。

' Gets the attributes for the property.
Dim attributes As AttributeCollection = _
    TypeDescriptor.GetProperties(Me)("MyImage").Attributes

' Prints the description by retrieving the DescriptionAttribute
' from the AttributeCollection. 
Dim myAttribute As DescriptionAttribute = _
    CType(attributes(GetType(DescriptionAttribute)), DescriptionAttribute)
Console.WriteLine(myAttribute.Description)
// Gets the attributes for the property.
 AttributeCollection attributes = 
    TypeDescriptor.GetProperties(this)["MyImage"].Attributes;
 
 /* Prints the description by retrieving the DescriptionAttribute 
  * from the AttributeCollection. */
 DescriptionAttribute myAttribute = 
    (DescriptionAttribute)attributes[typeof(DescriptionAttribute)];
 Console.WriteLine(myAttribute.Description);
// Gets the attributes for the property.
AttributeCollection^ attributes = TypeDescriptor::GetProperties( this )[ "MyImage" ]->Attributes;

/* Prints the description by retrieving the DescriptionAttribute 
      * from the AttributeCollection. */
DescriptionAttribute^ myAttribute = dynamic_cast<DescriptionAttribute^>(attributes[ DescriptionAttribute::typeid ]);
Console::WriteLine( myAttribute->Description );
// Gets the attributes for the property.
AttributeCollection attributes = TypeDescriptor.GetProperties(this).
    get_Item("MyImage").get_Attributes();

/* Prints the description by retrieving the DescriptionAttribute 
   from the AttributeCollection. 
 */
DescriptionAttribute myAttribute = (DescriptionAttribute)(attributes.
    get_Item(DescriptionAttribute.class.ToType()));

Console.WriteLine(myAttribute.get_Description());

继承层次结构

System.Object
   System.Attribute
    System.ComponentModel.DescriptionAttribute
       派生类

线程安全

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

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、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

请参见

参考

DescriptionAttribute 成员
System.ComponentModel 命名空间
Attribute
PropertyDescriptor
EventDescriptor