Code goes first,
namespace AttribTest { class Program { [AttributeUsage(AttributeTargets.Class)] class AttribOnClassAttribute : Attribute { } [AttributeUsage(AttributeTargets.Property|AttributeTargets.Field)] class AttribOnFieldAttribute : Attribute { } [AttribOnClass] class BaseClass { [AttribOnField] public virtual int PropertyToAttribute { get; set; } } [AttribOnClass] class DerivedClassWithAttrib : BaseClass { [AttribOnField] public override int PropertyToAttribute { get; set; } } class DerivedClassWithoutAttrib : BaseClass { public override int PropertyToAttribute { get; set; } } static void Main(string[] args) { bool b = typeof(BaseClass).IsDefined(typeof(AttribOnClassAttribute), true); Console.WriteLine("b = {0}", b); b = typeof(BaseClass).IsDefined(typeof(AttribOnClassAttribute), false); Console.WriteLine("b = {0}", b); b = typeof(DerivedClassWithAttrib).IsDefined(typeof(AttribOnClassAttribute), true); Console.WriteLine("b = {0}", b); b = typeof(DerivedClassWithAttrib).IsDefined(typeof(AttribOnClassAttribute), false); Console.WriteLine("b = {0}", b); b = typeof(DerivedClassWithoutAttrib).IsDefined(typeof(AttribOnClassAttribute), true); Console.WriteLine("b = {0}", b); b = typeof(DerivedClassWithoutAttrib).IsDefined(typeof(AttribOnClassAttribute), false); Console.WriteLine("b = {0}", b); b = typeof(BaseClass).GetProperty("PropertyToAttribute").IsDefined(typeof(AttribOnFieldAttribute), true); Console.WriteLine("b = {0}", b); b = typeof(BaseClass).GetProperty("PropertyToAttribute").IsDefined(typeof(AttribOnFieldAttribute), false); Console.WriteLine("b = {0}", b); b = typeof(DerivedClassWithAttrib).GetProperty("PropertyToAttribute").IsDefined(typeof(AttribOnFieldAttribute), true); Console.WriteLine("b = {0}", b); b = typeof(DerivedClassWithAttrib).GetProperty("PropertyToAttribute").IsDefined(typeof(AttribOnFieldAttribute), false); Console.WriteLine("b = {0}", b); b = typeof(DerivedClassWithoutAttrib).GetProperty("PropertyToAttribute").IsDefined(typeof(AttribOnFieldAttribute), true); Console.WriteLine("b = {0}", b); b = typeof(DerivedClassWithoutAttrib).GetProperty("PropertyToAttribute").IsDefined(typeof(AttribOnFieldAttribute), false); Console.WriteLine("b = {0}", b); } } }
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.
Some other typical methods that access attributes with inherit parameter (like GetCustomAttributes) behave similarly.
This implies in order to obtain attributes in a way different than how it is designed one might need to write his own functional modules using the existing attribute accessing methods along with reflection capabilities.
It seems the following article more or less affirms this
http://stackoverflow.com/questions/540749/can-a-c-sharp-class-inherit-attributes-from-its-interface