to avoid old-fashioned non-generic syntax when searching attributes of known type, 1 uses extension methods in system.reflection.customattributeextensions
class (since .net 4.5).
however appears fail if search attribute on return parameter of overridden method (or accessor of overridden property/indexer).
i experiencing .net 4.6.1.
simple reproduction (complete):
using system; using system.reflection; namespace reflectiontrouble { class b { //[return: mymark("in base class")] // uncommenting not public virtual int m() => 0; } class c : b { [return: mymark("in inheriting class")] // commenting away attribute not public override int m() => -1; } [attributeusage(attributetargets.returnvalue, allowmultiple = false, inherited = false)] // commenting away attributeusage not sealed class mymarkattribute : attribute { public string descr { get; } public mymarkattribute(string descr) { descr = descr; } public override string tostring() => $"mymark({descr})"; } static class program { static void main() { var derivedreturnval = typeof(c).getmethod("m").returnparameter; // usual new generic syntax (extension method in system.refelction namespace): var attr = derivedreturnval.getcustomattribute<mymarkattribute>(); // blows here, system.indexoutofrangeexception: index outside bounds of array. console.writeline(attr); // old non-generic syntax without extension method works: var attr2 = ((mymarkattribute[])(derivedreturnval.getcustomattributes(typeof(mymarkattribute), false)))[0]; // ok console.writeline(attr2); } } }
the code may "too long read", overridden method attribute on return parameter , obvious attempt retrieve attribute instance.
stack trace:
unhandled exception: system.indexoutofrangeexception: index outside bounds of array. @ system.attribute.getparentdefinition(parameterinfo param) @ system.attribute.internalparamgetcustomattributes(parameterinfo param, type type, boolean inherit) @ system.attribute.getcustomattributes(parameterinfo element, type attributetype, boolean inherit) @ system.attribute.getcustomattribute(parameterinfo element, type attributetype, boolean inherit) @ system.reflection.customattributeextensions.getcustomattribute[t](parameterinfo element) @ reflectiontrouble.program.main() in c:\mypath\program.cs:line 38
am doing wrong?
is bug, , if yes, well-known, , old bug?
this indeed looks bug. problem seems inheritance. work:
returnparameter.getcustomattribute<mymark>(inherit: false)
retrieving attributes have 2 code paths work bit differently: memberinfo.getcustomattribute
(older) , attribute.getcustomattribute
(newer , recommended). there generic extension methods, use latter, newer approach. difference between these indeed way handle inheritance. .net 1.0 ignored inherit
parameter properties, events , parameters. in order not break things, static methods on attribute
introduced in .net 2.0 (together bug).
seems neglected special-case return value parameter when going inheritance tree (see here). can open issue in github repo or connect bug.
Comments
Post a Comment