简体   繁体   中英

Turning off Lamba Expressions in Visual Studio 2010 for Code Coverage

When looking at code coverage data with Visual Studio 2010 I see the following output under a given namespace:

SomeClass1
SomeClass2
SomeClass2.< >c__DisplayClass1
SomeClass2.< >c__DisplayClass19
SomeClass2.< >c__DisplayClass28
SomeClass3
SomeClass3.< >c__DisplayClass2F
etc

If I expand out any of the entries with "DisplayClass" in it I see that it is a method that has a lambda expression in it. Due to so many lambda expressions it is difficult to get meaningful data from the code coverage results.

Is there anyway to clean this report up?

The functions generated from a lambda expression are the direct result of the code you wrote. They may come back in the report as having a deceptively high number of lines due to the complier's expansion, but you want to test that their behavior is correct. Therefore they should be included in the coverage report.

I also agree with the comments: code coverage shouldn't be taken as an exact measurement. I think of it as having one significant digit.

Having said all that… I think your best hope is the ExcludeFromCodeCoverage Attribute. It's fairly flexible, but is normally applied to a declaration. How you would apply it to a lambda isn't clear to me.

You can mark the class with the DebuggerNonUserCode attribute.

I don't know of any way to exclude particular method patterns, and the compiler is converting your lambda expressions to methods as you've noticed (for example: SomeClass3.< >c__DisplayClass2F)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM