The FilterablePropertyBase class which implements ICustomTypeDescriptor.
public class
FilterablePropertyBase : ICustomTypeDescriptor
{
protected
PropertyDescriptorCollection GetFilteredProperties(Attribute[] attributes)
{
PropertyDescriptorCollection
pdc = TypeDescriptor.GetProperties(this,attributes,true);
PropertyDescriptorCollection
finalProps = new PropertyDescriptorCollection(new
PropertyDescriptor[0]);
foreach(PropertyDescriptor
pd in pdc)
{
bool
include=false;
bool
Dynamic=false;
foreach(Attribute
a in pd.Attributes)
{
if(a
is DynamicPropertyFilterAttribute)
{
Dynamic=true;
DynamicPropertyFilterAttribute
dpf=(DynamicPropertyFilterAttribute)a;
PropertyDescriptor
temp=pdc[dpf.PropertyName];
if(dpf.ShowOn.IndexOf(temp.GetValue(this).ToString())>-1)
{
include=true;
}
}
}
if(!Dynamic
|| include)
finalProps.Add(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
GetFilteredProperties(attributes);
}
PropertyDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetProperties()
{
return
GetFilteredProperties(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
}
Public
Class FilterablePropertyBase
Implements ICustomTypeDescriptor
Protected Function
GetFilteredProperties(ByVal attributes
As Attribute()) As
PropertyDescriptorCollection
Dim
pdc As PropertyDescriptorCollection =
TypeDescriptor.GetProperties(Me, attributes,
True)
Dim
finalProps As PropertyDescriptorCollection =
New PropertyDescriptorCollection(New
PropertyDescriptor(0) {})
Dim
pd As PropertyDescriptor
For
Each pd In pdc
Dim
include As Boolean
= False
Dim
dynamic As Boolean
= False
Dim
a As Attribute
For
Each a In
pd.Attributes
If
TypeOf a Is
DynamicPropertyFilterAttribute Then
dynamic = True
Dim
dpf As DynamicPropertyFilterAttribute =
CType(a, DynamicPropertyFilterAttribute)
Dim
temp As PropertyDescriptor =
pdc(dpf.PropertyName)
If
dpf.ShowOn.IndexOf(temp.GetValue(Me).ToString())
> -1 Then
include = True
End
If
End
If
Next
If
Not dynamic Or
include Then
finalProps.Add(pd)
End
If
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
GetFilteredProperties(New Attribute() {})
End Function
Public Overloads
Function GetProperties1(ByVal
attributes() As System.Attribute)
As
System.ComponentModel.PropertyDescriptorCollection
Implements System.ComponentModel.ICustomTypeDescriptor.GetProperties
Return
GetFilteredProperties(attributes)
End Function
Public Function
GetPropertyOwner(ByVal pd
As System.ComponentModel.PropertyDescriptor)
As Object
Implements
System.ComponentModel.ICustomTypeDescriptor.GetPropertyOwner
Return
Me
End Function
End
Class
Use the Back button to return to the article.