Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

 

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

  • 4 weeks later...
Posted

Abstraction suggests implementation differences, so play it safe

 

When I call MyBase.New I send all the attributes for the new custom propertydescriptor as a constuctor argument' date=' so the information which the functions return should be obtainable from those surely?[/quote']

 

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:

Never trouble another for what you can do for yourself.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...