Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
DescriptionAttribute Class

Updated: November 2007

Specifies a description for a property or event.

Namespace:  System.ComponentModel
Assembly:  System (in System.dll)

Visual Basic (Declaration)
<AttributeUsageAttribute(AttributeTargets.All)> _
Public Class DescriptionAttribute _
    Inherits Attribute
Visual Basic (Usage)
Dim instance As DescriptionAttribute
C#
[AttributeUsageAttribute(AttributeTargets.All)]
public class DescriptionAttribute : Attribute
Visual C++
[AttributeUsageAttribute(AttributeTargets::All)]
public ref class DescriptionAttribute : public Attribute
J#
/** @attribute AttributeUsageAttribute(AttributeTargets.All) */
public class DescriptionAttribute extends Attribute
JScript
public class DescriptionAttribute extends Attribute

A visual designer can display the specified description when referencing the component member, such as in a Properties window. Call Description to access the value of this attribute.

For more information, see Attributes Overview and Extending Metadata Using Attributes.

The following example creates the MyImage property. The property has two attributes, a DescriptionAttribute and a CategoryAttribute.

Visual Basic
<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


C#
[Description("The image associated with the control"),Category("Appearance")] 
 public Image MyImage {
    get {
       // Insert code here.
       return image1;
    }
    set {
       // Insert code here.
    }
 }

Visual C++
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.
      }
   }

J#
/** @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

The next example gets the description of MyImage. First the code gets a PropertyDescriptorCollection with all the properties for the object. Next it indexes into the PropertyDescriptorCollection to get MyImage. Then it returns the attributes for this property and saves them in the attributes variable.

The example then prints the description by retrieving DescriptionAttribute from the AttributeCollection, and writing it to the console screen.

Visual Basic
' 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)

C#
// 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);

Visual C++
// 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 );

J#
// 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());

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker