I have a class which inherits from the PropertyDescriptor Class. I use this, among other things, to alter certain of the attributes of properties at run time. The PropertyDescriptor's Attribute collection is readonly at run time, so you need to create your own propertydescriptors from scratch to do this. If you inherit PropertyDescriptor, you have to override lots of its functions, and in order to return the correct value for each of those functions, I'm holding onto a reference to the original PropertyDescriptor in the custom one. This works, but it seems very duplicative and just wrong, intuitively. When I call MyBase.New I send all the attributes for the new custom propertydescriptor as a constuctor argument, so the information which the functions return should be obtainable from those surely? Below is the code I'm using, so hopefully you can see what I mean from it. Can anyone tell me how I should be doing this better?
Code:
Friend Class CustomPropertyDescriptor
Inherits System.ComponentModel.PropertyDescriptor
Private MyPD As PropertyDescriptor 'Reference to original propertydescriptor.
Public Sub New(ByVal f As PropertyDescriptor, ByVal aa() As Attribute)
'aa is the new set of attributes, adapted from the attributes of f.
MyBase.New(f.DisplayName, aa)
MyPD = f
End Sub
Public Overrides Function CanResetValue(ByVal component As Object) As Boolean
Return MyPD.CanResetValue(component)
End Function
Public Overrides ReadOnly Property ComponentType() As System.Type
Get
Return MyPD.ComponentType
End Get
End Property
Public Overrides Function GetValue(ByVal component As Object) As Object
Return MyPD.GetValue(component)
End Function
Public Overrides ReadOnly Property IsReadOnly() As Boolean
Get
Return MyPD.IsReadOnly
End Get
End Property
Public Overrides ReadOnly Property PropertyType() As System.Type
Get
Return MyPD.PropertyType
End Get
End Property
Public Overrides Sub ResetValue(ByVal component As Object)
MyPD.ResetValue(component)
End Sub
Public Overrides Sub SetValue(ByVal component As Object, ByVal value As Object)
MyPD.SetValue(component, value) 'this will call the Property Set method.
End Sub
End Class