c# - Do custom attribute classes inherit the "Inherited" AttributeUsage flag? -



c# - Do custom attribute classes inherit the "Inherited" AttributeUsage flag? -

consider next scenario:

a base of operations attribute class baseattribute has attributeusageattribute specifying not inheritable (inherited = false). a derived attribute class derivedattribute inherits base of operations attribute class. a base of operations domain class base has derived attribute applied. a domain class derived inheriting base of operations domain class asked custom attributes, including inherited attributes (inherit: true).

here corresponding code:

using system; using system.linq; namespace consoleapplication26 { class programme { static void main () { var attributes = typeof (derived).getcustomattributes (true); foreach (var attribute in attributes) { console.writeline ( "{0}: inherited = {1}", attribute.gettype().name, attribute.gettype().getcustomattributes (typeof (attributeusageattribute), true).cast<attributeusageattribute>().single().inherited); } } } [attributeusage (attributetargets.all, inherited = false)] public class baseattribute : attribute { } public class derivedattribute : baseattribute { } [derived] public class base of operations { } public class derived : base of operations { } }

in scenario, getcustomattributes api returns instance of derivedattribute class. have expected not homecoming instance because http://msdn.microsoft.com/en-us/library/system.attributeusageattribute.aspx says attributeusageattribute inheritable.

now, bug, or expected/documented somewhere?

note (2013-02-20): experiments show attributetargets part of baseattribute class indeed inherited derivedattribute class. e.g., when alter allowed targets on baseattribute attributetargets.method, c# compiler won't allow me apply derivedattribute class. therefore, doesn't create sense inherited = false part not inherited derivedattribute, , i'm inclined think of bug in implementation of getcustomattributes.

according metadata in .net 4.0, attributeusageattribute class marked [attributeusage(attributetargets.class, inherited = true)]. so, if attribute class (baseattribute, in example), has attributeusageattribute applied (as attribute classes should, don't break if don't- see below), classes derrived baseattribute should inherit attributeusage attribute applied it.

your derived class inherits derivedattribute base, because derivedattribute doesn't have own attributeusageattribute applied it, reflection api relies on baseattribute's attribute. now, take attributeusageattribute off of baseattribute class, , same result, because base of operations system.attribute class marked [attributeusage(attributetargets.all, inherited = true, allowmultiple = false)]. thus, attribute class inherit attribute, unless specify different one.

wow, complicated paragraphs. attributes on attributes makes heavy reading :p

c# reflection custom-attributes

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -