Can attribute be inherited with the class it applied to?

  • Thread starter Thread starter feng
  • Start date Start date
F

feng

Hi,

I have a class, say class A, that has <Serializable()>
attribute applied to it. Now if I create a class B that
inherits from A, does B automaticly inherits this
attribute also? or do I have to apply <Serializable()> to
B, if I want B to be serializable?

Thanks

Lifeng
 
I have a class, say class A, that has <Serializable()>
attribute applied to it. Now if I create a class B that
inherits from A, does B automaticly inherits this
attribute also?

Serializeble isn't inherited no. But Serializable is a special,
pseudo-custom attribute.

For regular custom attributes, it depends on the AttributeUsage
attribute on the attribute class (wheter it has Inherited=True set).



Mattias
 
feng,

note the difference between comment and compiled code. Current attribute
syntax is some kind of comment. While some comments are processed by
compiler, they are basically not the part of actual code. So, attributes
declared in comments or as part of declaration are not inheritable - at
least now. However, you can easily change that by adding attribute field
(property or internal variable) into base class.
Even if at first glance this is not what you want - because functionality
like adding/removing attributes dynamically is not yet supported afaik (btw
this could be next step in evolution of .Net), you will be able to use
inherited attributes in derived classes.

By the way, what could be the purpose of inheriting attribute(s)? To me it
looks more like self-evolving code paradigm, which is clearly not what you
look for.

However I might be a bit wrong here 'cause looked into attributes couple of
years ago during my Beta time :-()
Anybody knows better?

HTH
Alex
 
Back
Top