namespace AttribTest { class Program { [AttributeUsage(AttributeTargetsClass)] class AttribOnClassAttribute Attribute { }
[AttributeUsage(AttributeTargetsProperty|AttributeTargetsField)] class AttribOnFieldAttribute Attribute { }
[AttribOnClass] class BaseClass { [AttribOnField] public virtual int PropertyToAttribute { getset}
[AttribOnClass] class DerivedClassWithAttrib BaseClass { [AttribOnField] public override int PropertyToAttribute { getset}
class DerivedClassWithoutAttrib BaseClass { public override int PropertyToAttribute { getset}
static void Main(string[] args)
{ bool b = typeof(BaseClass)IsDefined(typeof(AttribOnClassAttribute) true)ConsoleWriteLine(b = {} b)
b = typeof(BaseClass)IsDefined(typeof(AttribOnClassAttribute) false)ConsoleWriteLine(b = {} b)
b = typeof(DerivedClassWithAttrib)IsDefined(typeof(AttribOnClassAttribute) true)ConsoleWriteLine(b = {} b)
b = typeof(DerivedClassWithAttrib)IsDefined(typeof(AttribOnClassAttribute) false)ConsoleWriteLine(b = {} b)
b = typeof(DerivedClassWithoutAttrib)IsDefined(typeof(AttribOnClassAttribute) true)ConsoleWriteLine(b = {} b)
b = typeof(DerivedClassWithoutAttrib)IsDefined(typeof(AttribOnClassAttribute) false)ConsoleWriteLine(b = {} b)
b = typeof(BaseClass)GetProperty(PropertyToAttribute)IsDefined(typeof(AttribOnFieldAttribute) true)ConsoleWriteLine(b = {} b)
b = typeof(BaseClass)GetProperty(PropertyToAttribute)IsDefined(typeof(AttribOnFieldAttribute) false)ConsoleWriteLine(b = {} b)
b = typeof(DerivedClassWithAttrib)GetProperty(PropertyToAttribute)IsDefined(typeof(AttribOnFieldAttribute) true)ConsoleWriteLine(b = {} b)
b = typeof(DerivedClassWithAttrib)GetProperty(PropertyToAttribute)IsDefined(typeof(AttribOnFieldAttribute) false)ConsoleWriteLine(b = {} b)
b = typeof(DerivedClassWithoutAttrib)GetProperty(PropertyToAttribute)IsDefined(typeof(AttribOnFieldAttribute) true)ConsoleWriteLine(b = {} b)
b = typeof(DerivedClassWithoutAttrib)GetProperty(PropertyToAttribute)IsDefined(typeof(AttribOnFieldAttribute) false)ConsoleWriteLine(b = {} b)}
The result of the execution with some comments and explanationsb = True b = True // attribute is defined right on the class so it returns true no matter if it searches through inheritance chain same applies to the two below b = True b = True // attribute is only defined on the base class however it specifies it should look through inheritance b = False // inheritance search is disabled so it returns false b = True b = True // again the attribute is defined explicitly b = True b = False // even if its on the method of base class and inheritance search is enabled it returns false method attributes work different than type attributes in inheritance b = False
Further study shows that
Named parameter Inherited= to AttributeUsage attribute defining attribute works as expected for class attribute definition (the default value being true) but not for member attributes (at least not in this particular sample) strange enough It seems the following article more or less affirms this