Inheritance of user attributes by abstract properties

I have a custom attribute that I want to apply to a base abstract class so that I can skip elements that the user does not need to see when displaying the element in HTML. It seems that properties overriding the base class do not inherit attributes.

Does the basic property (abstract or virtual) replace the attributes assigned to the original property?

From the Defination attribute class

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

From the abstract class Defination

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

From the definition of the class of concrete

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

From class validation to attribute

  Type t = o.GetType(); foreach (PropertyInfo pi in t.GetProperties()) { if (pi.GetCustomAttributes(typeof(NoHtmlOutput), true).Length == 1) continue; // processing logic goes here } 
+37
reflection c # abstract-class attributes
Mar 25 '10 at 23:00
source share
5 answers

No, attributes are inherited.

This is the GetCustomAttributes() method, which does not consider parent declarations. It only considers attributes that apply to the specified element. From the docs :

Note

This method ignores the inheritance parameter for properties and events. Search the inheritance chain for property attributes and events, use the appropriate overloads. .. :: attribute. GetCustomAttributes Method.

+25
Mar 25 '10 at 23:07
source share

Instead of calling PropertyInfo.GetCustomAttributes (...) you should 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); 
+54
Aug 24 2018-11-12T00:
source share

It looks like this only happens when the override method also has an attribute.

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

However, you can override attributes of the same type or apply additional attributes to a derived component. The following code snippet shows a custom control that overrides the Text property inherited from the control by overriding the BrowsableAttribute attribute used 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 
0
Mar 25 '10 at 23:14
source share

Here is my attempt. This is an extension method on MemberInfo that manually raises the inheritance hierarchy. This seems to be compatible with dynamic proxies ... at least the hose created by Castle, so I assume that it will be compatible with any proxy library.

 public static IEnumerable<T> GetCustomAttributes<T>(this MemberInfo instance) { while (instance != null) { object[] attributes = instance.GetCustomAttributes(typeof(T), false); if (attributes.Length > 0) { return attributes.Cast<T>(); } Type ancestor = instance.DeclaringType.BaseType; if (ancestor != null) { IEnumerable<MemberInfo> ancestormatches = ancestor.FindMembers(instance.MemberType, BindingFlags.Instance | BindingFlags.Public, (m, s) => { return m.Name == (string)s; }, instance.Name); instance = ancestormatches.FirstOrDefault(); } else { instance = null; } } return new T[] { }; } 

And you would use it like that.

 Type t = o.GetType(); foreach (PropertyInfo pi in t.GetProperties()) { IEnumerable<NoHtmlOutput> attributes = pi.GetCustomAttributes<NoHtmlOutput>(); foreach (NoHtmlOutput attribute in attributes) { Console.WriteLine(attribute); } } 
0
Nov 09 '16 at 20:23
source share

you can use

 PropertyInfo::GetCustomAttributes<T>(true) 

which works great, see example: https://dotnetfiddle.net/2IhEWH

therefore there is no need to use a static method

 System.Attribute.GetCustomAttributes 
0
Dec 01 '16 at 9:05
source share



All Articles