public - Yield Return in C# - makes fail tests for publicity of fields -



public - Yield Return in C# - makes fail tests for publicity of fields -

today encoutered problem connected c#. have write programme pass tests. implemented working correctly, there 1 test disallows using public fields, , fails.

i have generic class implementing ienumerable, , thing causes problem is:

public ienumerator<r> getenumerator() { foreach (r r in roomlist) yield homecoming r; }

if set class "return null", test passes fine. wonder can wrong? here test report:

result message: assert.fail failed. detected type "exercise.hotel`2+<getenumerator>d__0" public filed(s) "exercise.hotel`2[r,sc] <>4__this, r <r>5__1, enumerator <>7__wrap2", not allowed.

here code of test. honest not familiar how works. of course of study can caused bad test.

var withpublicfield = types.where(t => !t.isenum) .where(t => t.getfields(bindingflags.public | bindingflags.instance).count() > 0) .where(t => !t.name.startswith("<>c__displayclass")) .todictionary(t => t.fullname, t => t.getfields(bindingflags.public | bindingflags.instance));

thanks help. it's first question hope did :)

it's odd test talking about.

the reason iterator method (i.e. method yield return in it) triggers failure c# compiler rewrites such methods homecoming instance of hidden class implements method state machine. it's type has disallowed public fields in it.

personally, it's sentiment failure false positive. while agree public fields in general abomination, in case you're dealing hidden, compiler-generated code. particular scenario should fine.

hopefully can suppress failure in case, explanation of "it's not fault!" alternative implement own class implements ienumerator<t> type scenario, , of course of study not contain public fields.

edit: notice looking @ test code attempts exclude compiler-generate code captured variables (i.e. name.startswith("<>c__displayclass")). perhaps test author add together similar exclusion compiler-generated <getenumerator> types (or of course of study check [compilergenerated] per lee's answer).

c# public ienumerator

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -