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# 中,你可以为类、方法、属性或其他程序实体添加属性(Attributes)。属性是一种元数据,可以提供关于程序实体的额外信息,并可以在运行时通过反射来访问。要为一个程序实体添加属性,你需要使用方括号 [] 将属性放在目标实体的上方。 以下是一个简单的示例,展示如何在 C# 中添加属性: ```csharp using System; // 自定义属性类 public class MyAttribute : Attribute { public string Description { get; set; } public MyAttribute(string description) { Description = description; } } // 使用自定义属性 [MyAttribute("这是一个示例类")] public class MyClass { [MyAttribute("这是一个示例方法")] public void MyMethod() { // 方法的实现 } } class Program { static void Main(string[] args) { // 获取类上的属性 var classAttribute = (MyAttribute)Attribute.GetCustomAttribute(typeof(MyClass), typeof(MyAttribute)); Console.WriteLine(classAttribute.Description); // 获取方法上的属性 var methodInfo = typeof(MyClass).GetMethod("MyMethod"); var methodAttribute = (MyAttribute)Attribute.GetCustomAttribute(methodInfo, typeof(MyAttribute)); Console.WriteLine(methodAttribute.Description); } } ``` 在上述示例中,我们定义了一个名为 `MyAttribute` 的自定义属性类。然后,我们将 `MyAttribute` 属性应用于 `MyClass` 类和其中的 `MyMethod` 方法。在 `Main` 方法中,我们使用反射来获取并打印这些属性的描述信息。 请注意,属性可以具有不同的参数和返回类型,这取决于你的需求。你可以根据自己的需要定义和使用属性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值