How to change attribute in derived class

  • Thread starter Thread starter Michael Chambers
  • Start date Start date
M

Michael Chambers

Hi there,

Is there a clean way to change a property or method's attribute in a derived
class without redefining the property/method itself. That is, redefining the
property/method in the derived class simply to change its attribute is ugly
IMO. I assume you can do this and then simply defer to the base class
version but it's unwieldy so I'm wondering if there's a better way. Thanks
in advance.
 
Michael,

I agree with you, but there is no other way. There are some (mostly related
to the design time behavior and property grid) where using TypeConverter one
can return collection of PropertyDescriptors with changed set of attributes.
Again this works only in some special situations and only for properties.

What attributes do you want to change?

Whatever the case I believe overriding the base class methods is going to be
much easier.
 
Michael,
I agree with you, but there is no other way. There are some (mostly
related to the design time behavior and property grid) where using
TypeConverter one can return collection of PropertyDescriptors with
changed set of attributes. Again this works only in some special
situations and only for properties.

What attributes do you want to change?

Whatever the case I believe overriding the base class methods is going to
be much easier.

Thanks for the feedback. I've since changed my approach to eliminate this
need however (read on) but what I originally wanted to do was derive a new
class from "TreeView" (or any other control for that matter) where one or
more base class properties would then become constant (fixed for that
particular derivative). Since I wanted to add my inherited control to the
form designer "Toolbox" however (so I could drag it onto my forms), I would
have to set the "BrowsableAttribute" to "false" for these properties so they
would no longer appear in a form's properties window (since they're now
constant). I changed my mind however and decided to just create a
"UserControl" derivative instead so I'm now wrapping the original control
instead of inheriting from it. This eliminates the original problem as I can
now simply expose the wrapped control's functionality on an as-needed basis
(deferring to the underlying control when needed). Anyway, thanks again.
 
Michael,

If this is what you want to do you can use TypeConverter for that.
Create new TypeConverter by inheriting the TreeNodeConverter override
GetPropertiesSupported and return *true* then override GetProperties.
GetProperties is the method where you are going to change the attributes for
some of the proeprties. Using TypeDescriptor you can get collection of
PropertyDescriptors for the type properties. The you create a new
PropertyDescriptorCollection and transfer all the proeprties from the
original one to the new one. If you need to change the browsable attribute
for some property use TypeDescriptor.CreateProperty to create a new
PropertyDescriptor based on the original PropertyDescriptor, but change the
set of attributes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top