473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Attributes in base and derived classes

If I set an attribute on some part of a class, such as a property or event,
does that attribute also apply to the same property or event of a derived
class ?

If the above is true, can I change the attribute of a property or event from
something in a base class to something different in a derived class ?
Jul 21 '05 #1
2 2450
Edward,
If I set an attribute on some part of a class, such as a property or event,
does that attribute also apply to the same property or event of a derived
class ?


It depends on how you look up the attribute, the GetCustomAttribute
reflection methods usually have a boolean parameter to indicate
whether you want to search base class members for attributes as well.

It also depends on the AttributeUsage.Inherited setting of the
attribute class.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 21 '05 #2
Mattias Sjögren wrote:
Edward,
If I set an attribute on some part of a class, such as a property or
event, does that attribute also apply to the same property or event
of a derived class ?
It depends on how you look up the attribute, the GetCustomAttribute
reflection methods usually have a boolean parameter to indicate
whether you want to search base class members for attributes as well.


In that case I would have to know how the attribute is being used by
whomever is using it.

It also depends on the AttributeUsage.Inherited setting of the
attribute class.


Amazingly enough one topic in the MSDN documentation says that the default
value for the Inherited value of AttributeUsageAttribute class is true ( in
the actual .NET framework reference for the class ), while another topic
says that the default value for the Inherited value of the
AttributeUsageAttribute class is false ( C# Programmer's Reference, C#
Attributes ). I tend to believe the former is true, as it is more natural to
believe that if one sets an attribute on an element in a base class, the
same attribute should be applied to the inherited element of the derived
class.

This whole question came about because I wished to be able to change a
property or event in a base class from being browsable to being not
browsable in a derived class. The BrowsableAttribute normally defaults to
true. I was wondering how I can set it to false for a derived class,
especially as I can't just redeclare a given property or event. Is there a
way to apply an attribute to an element of a base class which has been
automatically derived in a derived class ?
Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
5
by: Nathan Bullock | last post by:
Hi, I have a base class, say Base and there are two classes, say Class1 and Class2 which are derived from Base. Is there any way for me, say from a static method in Base, to get a list of all...
1
by: ViperDK \(Daniel K.\) | last post by:
I want to make my own attributes that should only be used in derived classes so the base class can get some metadata of fields in derived classes. for that reason i try to define a new attribute...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
2
by: Edward Diener | last post by:
If I set an attribute on some part of a class, such as a property or event, does that attribute also apply to the same property or event of a derived class ? If the above is true, can I change...
15
by: Jeff Mason | last post by:
Hi, I'm having a reflection brain fog here, perhaps someone can set me on the right track. I'd like to define a custom attribute to be used in a class hierarchy. What I want to do is to...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
2
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.