C# Attributes Inheritance (A brief look)

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);
        }
    }
}

The result of the execution with some comments and explanations,

b = 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
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
b = True // again the attribute is defined explicitly
b = True
b = False // even if it's 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.

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











posted @ 2011-10-18 07:41  quanben  阅读(169)  评论(0编辑  收藏  举报