views:

142

answers:

2

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
        }
+1  A: 

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.

womp
Searching the Chain by providing the inherit parameter pi.GetCustomAttributes(typeof(NoHtmlOutput), true)
Marty Trenouth
Read the remarks. PropertyInfo.GetCustomAttributes() *ignores* the inherit parameter.
womp
Never mind.... Microsoft had to put it in the smallest type face possible
Marty Trenouth
A: 

Looks like it only happens when the overriding method also has the attribute .

http://msdn.microsoft.com/en-us/library/a19191fh.aspx

However, you can override attributes of the same type or apply additional attributes to the derived component. The following code fragment shows a custom control that overrides the Text property inherited from Control by overriding the BrowsableAttribute attribute applied in the base class. Visual Basic

Public Class MyControl
   Inherits Control
   ' The base class has [Browsable(true)] applied to the Text property.
   <Browsable(False)>  _
   Public Overrides Property [Text]() As String
      ...
   End Property 
   ...
End Class
eschneider