In Depth Banner
Skip Navigation Links

Select your preferred language

The FriendlyNameBase and DemoClass derived from it.

 

  //A base class that can be used to create derived classes which use friendly names

  //The methods and properties normally return data from the standard TypeDescriptor

  //except the methods that return a PropertyDescriptorCollection.

  public class FriendlyNameBase : ICustomTypeDescriptor

  {

 

    /// <summary>

    /// Creates a collection of FriendlyPropertyDescriptors

    /// </summary>

    /// <param name="attributes">An array of attributes used to select the properties required.

    /// Normally contains a BrowsableAttribute used to select only the browsable properties from the class</param>

    /// <returns>A collection of modified property descriptors</returns>

    protected PropertyDescriptorCollection GetFriendlyProperties(Attribute[] attributes)

    {

      PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(this,attributes,true);

      PropertyDescriptorCollection finalProps=new PropertyDescriptorCollection(new PropertyDescriptor[0]);

      foreach(PropertyDescriptor pd in pdc)

        finalProps.Add(new FriendlyNamePropertyDescriptor(pd));

      return finalProps;

    }

 

    #region ICustomTypeDescriptor Members

 

    public TypeConverter GetConverter()

    {

      return TypeDescriptor.GetConverter(this,true);

    }

 

    public EventDescriptorCollection GetEvents(Attribute[] attributes)

    {

      return TypeDescriptor.GetEvents(this,attributes,true);

    }

 

    EventDescriptorCollection System.ComponentModel.ICustomTypeDescriptor.GetEvents()

    {

      return TypeDescriptor.GetEvents(this,true);

    }

 

    public string GetComponentName()

    {

      return TypeDescriptor.GetComponentName(this,true);

    }

 

    public object GetPropertyOwner(PropertyDescriptor pd)

    {

      return this;

    }

 

    public AttributeCollection GetAttributes()

    {

      return TypeDescriptor.GetAttributes(this,true);

    }

 

    public PropertyDescriptorCollection GetProperties(Attribute[] attributes)

    {

      return this.GetFriendlyProperties(attributes);

    }

 

    PropertyDescriptorCollection System.ComponentModel.ICustomTypeDescriptor.GetProperties()

    {

      return this.GetFriendlyProperties(new Attribute[0]);

    }

 

    public object GetEditor(Type editorBaseType)

    {

      return TypeDescriptor.GetEditor(this,editorBaseType,true);

    }

 

    public PropertyDescriptor GetDefaultProperty()

    {

      return TypeDescriptor.GetDefaultProperty(this,true);

    }

 

    public EventDescriptor GetDefaultEvent()

    {

      return TypeDescriptor.GetDefaultEvent(this,true);

    }

 

    public string GetClassName()

    {

      return TypeDescriptor.GetClassName(this,true);

    }

 

    #endregion

 

  }

 

//////////////////////////////////////////////////////////////////////////////////////

 

  //A class based on the FriendlyNameBase which is used

  //to populate the property grid and display friendly named properties

 

  public class DemoClass : FriendlyNameBase

  {

    string _notAVeryFriendlyName;

    public string NotAVeryFriendlyName

    {

      get{return _notAVeryFriendlyName;}

      set{_notAVeryFriendlyName=value;}

    }

 

    string _aVeryFriendlyNameIndeed;

    [FriendlyName("A Friendly Name")] //Note the use of this attribute

    public string AVeryFriendlyNameIndeed

    {

      get{return _aVeryFriendlyNameIndeed;}

      set{_aVeryFriendlyNameIndeed=value;}

    }

  }

 

Use the Back button to return to the article

Copyright © Bob Powell 2003-2009. All rights reserved