Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Method Attributes Inherited in C#?

Are attributes applied to an abstract method in a base class applied to the overridden versions in the child classes?

I hope the question is clear enough without an example.

like image 778
Ali Kazmi Avatar asked Jul 14 '09 05:07

Ali Kazmi


People also ask

Can attributes be inherited?

When you associate an attribute with a product class, it is inherited by all member subclasses. If you edit an attribute on the product class where it was originally defined, the changes propagate to all member subclasses. The attribute definition is uniform for all subclasses that inherit it.

Are attributes inherited C#?

The default is false (single-use). The parameter inherited (optional) provides value for the Inherited property of this attribute, a Boolean value. If it is true, the attribute is inherited by derived classes.

Is AttributeUsage inherited?

A base attribute class BaseAttribute has an AttributeUsageAttribute specifying that it is not inheritable ( Inherited = False ). A derived attribute class DerivedAttribute inherits from that base attribute class.

Are attributes inherited C++?

Basically: an attribute is not a member of the class or a constructor, so it can't be inherited.


2 Answers

It depends on how the attribute itself is declared - see AttributeUsageAttribute.Inherited property.

like image 72
Pavel Minaev Avatar answered Oct 09 '22 01:10

Pavel Minaev


It depends on the Attribute.

attributes applied to Attribute class defination, carry a property [AttributeUsageAttribute.Inherited] that determines if an attributed is inherited in the derived classes.

check out this sample

[global::System.AttributeUsage(AttributeTargets.Method, Inherited = true,       AllowMultiple = false)] public sealed class MyAttribute : Attribute {      public MyAttribute (string FieldName)     {         //constructor.     }  } 
like image 29
this. __curious_geek Avatar answered Oct 09 '22 02:10

this. __curious_geek