Click here to Skip to main content
Click here to Skip to main content

Referenced Description Attribute

By , 6 May 2008
 

Introduction

This attribute shows the existing description of another control and a specific property instead of writing the same text again.

Background

The .NET Framework contains many descriptions that are shown in designer. I didn't want to write the same text again for another control.

Using the Code

Add the source file to a specific assembly, e.g. FrameworkExtensions.dll, and add a reference to this assembly.

Use the ReferencedDescriptionAttribute at the top of your control's class calling the referenced type and the specific property.

Example: You developed a DataGridViewMaskedTextColumn with your own property Mask. You want the description to match the description of MaskedTextBox's property Mask. Then you define:

[ReferencedDescription(typeof(System.Windows.Forms.MaskedTextBox), "Mask")]

How It Works

The ReferencedDescriptionAttribute class is derived from the DescriptionAttribute class. The only constructor needs two arguments:

  1. The referenced type
  2. The referenced property
using System;
using System.ComponentModel;

public class ReferencedDescriptionAttribute : DescriptionAttribute
{
  public ReferencedDescriptionAttribute(Type referencedType, string propertyName)
  {
    //  default result string
    string result = "Referenced description not available";
  }
} 

First, we get the properties of the referenced type. If they exist, we get the PropertyDescriptor to the specific property.

    PropertyDescriptorCollection properties
            = TypeDescriptor.GetProperties(referencedType);

    if (properties != null) {
      PropertyDescriptor property = properties[propertyName];
      if (property != null) {

Next, we get all the attributes to the property. The DescriptionAttribute is always available with an empty string by default. If it's not empty, we use this description as reference:

        AttributeCollection attributes = property.Attributes;
        DescriptionAttribute descript
          = (DescriptionAttribute)attributes[typeof(DescriptionAttribute)];

        // register the referenced description
        if (!String.IsNullOrEmpty(descript.Description))
          result = descript.Description;
      }
    }
    DescriptionValue = result;

That's all.

History

  • 05/03/2008 First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

J.Thomas
Germany Germany
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralWell DonememberYildirim Kocdag11 May '08 - 20:36 
Good job. Very useful article.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 6 May 2008
Article Copyright 2008 by J.Thomas
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid