How to disable special code analysis for the whole class

I am trying to disable the code analysis rule for the whole class, but NOT for the whole project, just one class. In the example below, the assembly generates warning CA1822 because it considers the unit test methods to be static.

The fix is ​​to add the following attribute to each unit test method: [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]

However, this is cumbersome and will interfere with a class with many unit tests.

I tried:

  • Moving an attribute to a class
  • Wrap all methods in

#pragma warning disable CA1822

#pragma warning restore CA1822

None of these two approaches worked.

 public class TestClass { public TestClass() { // some setup here } [Fact] public void My_Unit_Test1() { // the 'this' parameter is never used, causes CA warning 1822 } [Fact] public void My_Unit_Test2() { // the 'this' parameter is never used, causes CA warning 1822 } } 

Using VS2015 Update 2, .net 4.61 and new code analyzers.

+7
c # visual-studio-2015 code-analysis
source share
2 answers

This is not exactly what you want, but it can be less evil than the situation you have.

You can say that code analysis ignores a specific class with the following attribute:

 [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] 

Please note that your project must have a Visual Studio option "Disable Results from Generated Code (Managed Only)".

While annoying, you can still analyze the code in the file if you temporarily comment on this attribute.

+3
source share

Right-click the error in the error list tab, and you can select "In source" and "In suppression file".

SuppressMessageAttibute will be added to the source code (method or class level) if you select "In Source".

'[assembly: SUppressMessage' will be added to the GlobalSupressions.cs file, and you can configure the "Target" of this attribute.

snapshot here

0
source share

All Articles