PropertyDescriptor Inheritance

rbulph

Junior Contributor
Joined
Feb 17, 2003
Messages
397
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
 
Abstraction suggests implementation differences, so play it safe

rbulph said:
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?

The very fact that PropertyDescriptor is abstract, and that there are multiple subclasses, suggests that the behaviour of these properties and methods is not something that can be inferred from merely attributes, and depend upon the specific implementation of the PropertyDescriptor. If there were some possible 'default' implementation then these members would be virtual, not abstract.

As for whether keeping a wrapped copy is right or wrong, in this case it is probably the only way to be sure that these properties and methods are behaving correctly, as there may be some differences between different subclasses of PropertyDescriptor. The PropertyDescriptor does not appear to be a very 'bulky' class so I doubt this would cause any efficiency issues.

In the MSDN documentation for these abstract methods, many have hints as to how they might be implemented, so if you're really bothered by this then you could implement them, though some seem quite involved. As for IsReadOnly and PropertyType, their values can be stored in class variables.

Good luck :cool:
 
Back
Top