//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;}
}
}
Public
Class FriendlyNameBase
Implements ICustomTypeDescriptor
Protected Function
GetFriendlyProperties(ByVal attributes
As Attribute())
Dim
pdc As PropertyDescriptorCollection =
TypeDescriptor.GetProperties(Me, attributes,
True)
Dim
finalProps As New
PropertyDescriptorCollection(New
PropertyDescriptor() {})
Dim
pd As PropertyDescriptor
For
Each pd In pdc
finalProps.Add(New
FriendlyNamePropertyDescriptor(pd))
Next
Return
finalProps
End Function
Public Function
GetAttributes() As
System.ComponentModel.AttributeCollection Implements
System.ComponentModel.ICustomTypeDescriptor.GetAttributes
Return
TypeDescriptor.GetAttributes(Me,
True)
End Function
Public Function
GetClassName() As
String Implements
System.ComponentModel.ICustomTypeDescriptor.GetClassName
Return
TypeDescriptor.GetClassName(Me,
True)
End Function
Public Function
GetComponentName() As
String Implements
System.ComponentModel.ICustomTypeDescriptor.GetComponentName
Return
TypeDescriptor.GetComponentName(Me,
True)
End Function
Public Function
GetConverter() As
System.ComponentModel.TypeConverter Implements
System.ComponentModel.ICustomTypeDescriptor.GetConverter
Return
TypeDescriptor.GetConverter(Me,
True)
End Function
Public Function
GetDefaultEvent() As
System.ComponentModel.EventDescriptor Implements
System.ComponentModel.ICustomTypeDescriptor.GetDefaultEvent
Return
TypeDescriptor.GetDefaultEvent(Me,
True)
End Function
Public Function
GetDefaultProperty() As System.ComponentModel.PropertyDescriptor
Implements
System.ComponentModel.ICustomTypeDescriptor.GetDefaultProperty
Return
TypeDescriptor.GetDefaultProperty(Me,
True)
End Function
Public Function
GetEditor(ByVal editorBaseType
As System.Type) As
Object Implements
System.ComponentModel.ICustomTypeDescriptor.GetEditor
Return
TypeDescriptor.GetEditor(Me, editorBaseType,
True)
End Function
Public Overloads
Function GetEvents() As
System.ComponentModel.EventDescriptorCollection
Implements System.ComponentModel.ICustomTypeDescriptor.GetEvents
Return
TypeDescriptor.GetEvents(Me,
True)
End Function
Public Overloads
Function GetEvents1(ByVal
attributes() As System.Attribute)
As
System.ComponentModel.EventDescriptorCollection
Implements System.ComponentModel.ICustomTypeDescriptor.GetEvents
Return
TypeDescriptor.GetEvents(Me, attributes,
True)
End Function
Public Overloads
Function GetProperties()
As
System.ComponentModel.PropertyDescriptorCollection
Implements System.ComponentModel.ICustomTypeDescriptor.GetProperties
Return
GetFriendlyProperties(New Attribute() {})
End Function
Public Overloads
Function GetProperties1(ByVal
attributes() As System.Attribute)
As
System.ComponentModel.PropertyDescriptorCollection
Implements System.ComponentModel.ICustomTypeDescriptor.GetProperties
Return
GetFriendlyProperties(attributes)
End Function
Public Function
GetPropertyOwner(ByVal pd
As System.ComponentModel.PropertyDescriptor)
As Object
Implements
System.ComponentModel.ICustomTypeDescriptor.GetPropertyOwner
Return
Me
End Function
Public Sub
New()
End Sub
End
Class
Public
Class DemoClass
Inherits FriendlyNameBase
Private _notAVeryFriendlyName
As String
Public Property
NotAVeryFriendlyName() As
String
Get
Return
_notAVeryFriendlyName
End
Get
Set(ByVal
Value As String)
_notAVeryFriendlyName
= Value
End
Set
End Property
Private _friendlyName
As String
<FriendlyName("A
Friendly Name")> _
Public Property
FriendlyName() As
String
Get
Return
Me._friendlyName
End
Get
Set(ByVal
Value As String)
Me._friendlyName
= Value
End
Set
End Property
End
Class