Walkthrough: Analyzing Managed Code for Code Defects
In this walkthrough, you analyze a managed project for code defects by using the code analysis tool.
This walkthrough will step you through the process of using code analysis to analyze your .NET managed code assemblies for conformance with the Microsoft .NET Framework Design Guidelines.
In this walkthrough, you:
- Analyze and correct code defect warnings.
Prerequisites
- Visual Studio Premium.
Create a class library
To create a class library |
|
Analyze the project
To analyze a managed project for code defects
Select the CodeAnalysisManagedDemo project in Solution Explorer.
On the Project menu, click Properties.
The CodeAnalysisManagedDemo properties page is displayed.
Click Code Analysis.
Select Enable Code Analysis on Build (defines CODE_ANALYSIS constant).
From the Run this rule set drop-down list, select Microsoft All Rules.
On the File menu, click Save Selected Items, and then close the ManagedDemo properties pages.
On the Build menu, click Build ManagedDemo.
The CodeAnalysisManagedDemo project build warnings are reported in the Error List and Output windows.
Correct the Code Analysis Issues
To correct code analysis rule violations
On the View menu, click Error List.
Depending on the developer profile that you chose, you might have to point to Other Windows on the View menu, and then click Error List.
In Solution Explorer, click Show All Files.
Next, expand the Properties node, and then open the AssemblyInfo.cs file.
Use the following table to correct warnings:
Warnings |
To correct warning |
---|---|
CA1014: Mark assemblies with CLSCompliantAttribute: Microsoft.Design: 'demo' should be marked with the CLSCompliantAttribute, and its value should be true. |
|
CA1032: Implement standard exception constructors: Microsoft.Design: Add the following constructor to this class: public demo(String) |
|
CA1032: Implement standard exception constructors: Microsoft.Design: Add the following constructor to this class: public demo(String, Exception) |
|
CA1032: Implement standard exception constructors: Microsoft.Design: Add the following constructor to this class: protected demo(SerializationInfo, StreamingContext) |
|
CA1032: Implement standard exception constructors: Microsoft.Design: Add the following constructor to this class: public demo() |
|
CA1709: Identifiers should be cased correctly: Microsoft.Naming: Correct the casing of namespace name 'testCode' by changing it to 'TestCode'. |
|
CA1709: Identifiers should be cased correctly: Microsoft.Naming: Correct the casing of type name 'demo' by changing it to 'Demo'. |
|
CA1709: Identifiers should be cased correctly: Microsoft.Naming: Correct the casing of member name 'item' by changing it to 'Item'. |
|
CA1710: Identifiers should have correct suffix: Microsoft.Naming: Rename 'testCode.demo' to end in 'Exception'. |
|
CA2210: Assemblies should have valid strong names: Sign 'ManagedDemo' with a strong name key. |
|
CA2237: Mark ISerializable types with SerializableAttribute: Microsoft.Usage: Add a [Serializable] attribute to type 'demo' as this type implements ISerializable. |
|
Exclude Code Analysis Warnings
To exclude code defect warnings
Select the remaining warnings in the Error List, and then right-click, point to click Suppress Message(s) and then click In Project Suppression File. For more information, see How to: Suppress Warnings by Using the Menu Item
Rebuild the project.
The project builds without any warnings or errors.