quanben

quanben的技术空间

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











阅读更多
个人分类: C# 编码偶记
想对作者说点什么? 我来说一句

Bjarne Stroustrup语录.rar

2009年03月03日 406KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭