Attributes

M

Magne Ryholt

Suppose a class derived e.g. from System.Windows.Forms.TextBox with some
additional functionalities.
In my derived class I want (for some odd reason) to not beeing able to set
the Text property in design-time, this can be done by setting the
BrowsableAttribute to false.
How to achieve this ? Do I have to override the Text property with
[Browsable(false)] as attribute ? if so, then I "lose" other attributes set
for the base class's property (perhaps not an issue in this case).

My general question is: Is it possible to change an attribute defined in a
base class (supposing AttributeUsageAttribute, property Inherited is set to
true) ?
 
E

Eric Cadwell

If multiple attributes are applied at the base level, only those that are
overidden in the derived class are overidden. The remaining attributes are
still applied (if marked as Inherited as you mentioned).

To then remove an attribute you would need to derive the base once to mark
the desired attribute Inherited=false, then derive again.

Or

When reading the attributes with reflection, pass false to
GetCustomAttributes to instruct it not to search the inheritance chain for
attributes.

If you test the TextBox class (and it's inheritance chain) with reflection,
you see that TextBox.Text is not marked with any custom attributes. After
setting Browsable(false) it reports one attribute:

property name: Text
attribute: System.ComponentModel.BrowsableAttribute

HTH,
Eric Cadwell
http://www.origincontrols.com
 

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

Top