熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C# Attributes Inheritance

2013-11-13 10:05:43  來源: .NET編程 

  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


From:http://tw.wingwit.com/Article/program/net/201311/12536.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.