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











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值