i have basic question here. have attribute on abstract method in base class. when implement/override method in derived class not see attribute being applied in il generated derived class method. works fine.
am missing here? how know compiler has marked derived class method implementation decorated specific attribute?
any hints?
if apply attribute has inherited = true set in attributeusage attribute, , call getcustomattributes(inherit: true) on member inherits member attribute, you're going attribute. you're not going see in il of inheriting member, compiler doesn't special it, it's reflection looks @ base member.
for example code:
[attributeusage(attributetargets.all, inherited = true)] class inheritedattribute : attribute {} [attributeusage(attributetargets.all, inherited = false)] class notinheritedattribute : attribute {} abstract class base { [inherited, notinherited] public abstract void m(); } class derived : base { public override void m() {} } … foreach (var type in new[] { typeof(base), typeof(derived) }) { var method = type.getmethod("m"); foreach (var inherit in new[] { true, false }) { var attributes = method.getcustomattributes(inherit); console.writeline( "{0}.{1}, inherit={2}: {3}", method.reflectedtype.name, method.name, inherit, string.join(", ", attributes.select(a => a.gettype().name))); } } you're going output:
base.m, inherit=true: notinheritedattribute, inheritedattribute base.m, inherit=false: notinheritedattribute, inheritedattribute derived.m, inherit=true: inheritedattribute derived.m, inherit=false:
Comments
Post a Comment