c#

位置:IT落伍者 >> c# >> 浏览文章

C# Attributes Inheritance


发布日期:2023年05月14日
 
C# Attributes Inheritance

namespace AttribTest { class Program { [AttributeUsage(AttributeTargetsClass)] class AttribOnClassAttribute Attribute { }

[AttributeUsage(AttributeTargetsProperty|AttributeTargetsField)] class AttribOnFieldAttribute Attribute { }

[AttribOnClass] class BaseClass { [AttribOnField] public virtual int PropertyToAttribute { getset}

[AttribOnClass] class DerivedClassWithAttrib BaseClass { [AttribOnField] public override int PropertyToAttribute { getset}

class DerivedClassWithoutAttrib BaseClass { public override int PropertyToAttribute { getset}

static void Main(string[] args)

{ bool b = typeof(BaseClass)IsDefined(typeof(AttribOnClassAttribute) true)ConsoleWriteLine(b = {} b)

b = typeof(BaseClass)IsDefined(typeof(AttribOnClassAttribute) false)ConsoleWriteLine(b = {} b)

b = typeof(DerivedClassWithAttrib)IsDefined(typeof(AttribOnClassAttribute) true)ConsoleWriteLine(b = {} b)

b = typeof(DerivedClassWithAttrib)IsDefined(typeof(AttribOnClassAttribute) false)ConsoleWriteLine(b = {} b)

b = typeof(DerivedClassWithoutAttrib)IsDefined(typeof(AttribOnClassAttribute) true)ConsoleWriteLine(b = {} b)

b = typeof(DerivedClassWithoutAttrib)IsDefined(typeof(AttribOnClassAttribute) false)ConsoleWriteLine(b = {} b)

b = typeof(BaseClass)GetProperty(PropertyToAttributeIsDefined(typeof(AttribOnFieldAttribute) true)ConsoleWriteLine(b = {} b)

b = typeof(BaseClass)GetProperty(PropertyToAttributeIsDefined(typeof(AttribOnFieldAttribute) false)ConsoleWriteLine(b = {} b)

b = typeof(DerivedClassWithAttrib)GetProperty(PropertyToAttributeIsDefined(typeof(AttribOnFieldAttribute) true)ConsoleWriteLine(b = {} b)

b = typeof(DerivedClassWithAttrib)GetProperty(PropertyToAttributeIsDefined(typeof(AttribOnFieldAttribute) false)ConsoleWriteLine(b = {} b)

b = typeof(DerivedClassWithoutAttrib)GetProperty(PropertyToAttributeIsDefined(typeof(AttribOnFieldAttribute) true)ConsoleWriteLine(b = {} b)

b = typeof(DerivedClassWithoutAttrib)GetProperty(PropertyToAttributeIsDefined(typeof(AttribOnFieldAttribute) false)ConsoleWriteLine(b = {} b)}

The result of the execution with some comments and explanationsb = 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 // 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 // again the attribute is defined explicitly b = True b = False // even if its 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 It seems the following article more or less affirms this

               

上一篇:C# 语言规范--2.2.1 文法表示法

下一篇:使用.NET 向浏览器写入二进制文件