Code Analysis for Managed Code Overview
Note
This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here
Code analysis for managed code analyzes managed assemblies and reports information about the assemblies, such as violations of the programming and design rules set forth in the Microsoft .NET Framework Design Guidelines.
The analysis tool represents the checks it performs during an analysis as warning messages. Warning messages identify any relevant programming and design issues and, when it is possible, supply information about how to fix the problem.
IDE (integrated development environment) Integration
As a developer, you can run code analysis on your project automatically or you can run it manually.
To run code analysis each time that you build a project, you select Enable Code Analysis on Build (defines CODE_ANALYSIS constant) on the project's Property Page. For more information, see How to: Enable and Disable Automatic Code Analysis.
To run code analysis manually on a project, on the Analyze menu, click Run Code Analysis onProjectName. For more information, see How to: Enable and Disable Automatic Code Analysis.
Rule Sets
Code analysis rules for managed code are grouped into rule sets. You can use one of the Microsoft standard rule sets, or you can create a custom rule set to fulfill a specific need. For more information, see Using Rule Sets to Group Code Analysis Rules.
In Source Suppression
Frequently, it is useful to indicate that a warning is non-applicable. This informs the developer, and other people who might review the code later, that a warning was investigated and then either suppressed or ignored.
In Source Suppression of warnings is implemented through custom attributes. To suppress a warning, add the attribute SuppressMessage
to the source code as shown in the following example:
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1039:ListsAreStrongTyped")]
Public class MyClass
{
// code
}
For more information, see Suppress Warnings By Using the SuppressMessage Attribute.
Run code analysis as part of check-in policy
As an organization, you might want to require that all check-ins satisfy certain policies. In particular, you want to make sure that you follow these policies:
There were no build errors in code being checked in.
Code analysis was run as part of the most recent build.
You can accomplish this by specifying check-in policies. For more information, see Enhancing Code Quality with Team Project Check-in Policies.
Team Build Integration
You can use the integrated features of the build system to run the analysis tool as part of the build process. For more information, see Build the application.
See Also
Using Rule Sets to Group Code Analysis Rules How to: Enable and Disable Automatic Code Analysis