Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance of Custom Attributes on Abstract Properties

I've got a custom attribute that I want to apply to my base abstract class so that I can skip elements that don't need to be viewed by the user when displaying the item in HTML. It seems that the properties overriding the base class are not inheriting the attributes.

Does overriding base properties (abstract or virtual) blow away attributes placed on the original property?

From Attribute class Defination

[AttributeUsage(AttributeTargets.Property,                 Inherited = true,                 AllowMultiple = false)] public class NoHtmlOutput : Attribute { } 

From Abstract Class Defination

[NoHtmlOutput] public abstract Guid UniqueID { get; set; } 

From Concrete Class Defination

public override Guid UniqueID{ get{ return MasterId;} set{MasterId = value;}} 

From class checking for attribute

        Type t = o.GetType();         foreach (PropertyInfo pi in t.GetProperties())         {             if (pi.GetCustomAttributes(typeof(NoHtmlOutput), true).Length == 1)                 continue;             // processing logic goes here         } 
like image 316
Marty Trenouth Avatar asked Mar 25 '10 23:03

Marty Trenouth


People also ask

Can abstract classes have abstract attributes?

An abstract class can contain attribute or variables, abstract method, and normal method or anyone of them. But the subclass of the abstract class can only implement the abstract method of the abstract class.

What are abstract properties?

An abstract property declaration does not provide an implementation of the property accessors -- it declares that the class supports properties, but leaves the accessor implementation to derived classes. The following example demonstrates how to implement the abstract properties inherited from a base class.

Can you inherit attributes?

Subclasses can have two kinds of attributes: local and inherited. A local attribute is one that is defined on the subclass. An inherited attribute is one that is inherited from a parent product class. You customize an inherited attribute domain by editing its definition at the subclass level.

Can we declare properties in abstract class C#?

How to declare abstract properties in C# An abstract property is declared by using the abstract modifier in a property declaration to indicate that the property is an abstract method and does not contain implementation.


2 Answers

Instead of calling PropertyInfo.GetCustomAttributes(...), you have to call the static method System.Attribute.GetCustomAttributes(pi,...), as in:

PropertyInfo info = GetType().GetProperties();  // this gets only the attributes in the derived class and ignores the 'true' parameter object[] DerivedAttributes = info.GetCustomAttributes(typeof(MyAttribute),true);  // this gets all of the attributes up the heirarchy object[] InheritedAttributes = System.Attribute.GetCustomAttributes(info,typeof(MyAttribute),true); 
like image 106
John Holliday Avatar answered Sep 19 '22 13:09

John Holliday


No, attributes are inherited.

It's the GetCustomAttributes() method that does not look at parent declarations. It only looks at attributes applied to the specified member. From the docs:

Remarks

This method ignores the inherit parameter for properties and events. To search the inheritance chain for attributes on properties and events, use the appropriate overloads of the Attribute..::.GetCustomAttributes method.

like image 37
womp Avatar answered Sep 21 '22 13:09

womp