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 CodeAnalysis.
Make sure that Enable Code Analysis on Build (defines CODE_ANALYSIS constant) is checked.
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 Code Analysis and Output windows.
If the Code Analysis window does not appear, on the Analyze menu, choose Windows and then choose Code Analysis.
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. |
|
After you complete the changes, the Class1.cs file should look like the following:
//CodeAnalysisManagedDemo
//Class1.cs
using System;
using System.Runtime.Serialization;
namespace TestCode
{
[Serializable()]
public class DemoException : Exception
{
public DemoException () : base() { }
public DemoException(String s) : base(s) { }
public DemoException(String s, Exception e) : base(s, e) { }
protected DemoException(SerializationInfo info, StreamingContext context) : base(info, context) { }
public static void Initialize(int size) { }
protected static readonly int _item;
public static int Item { get { return _item; } }
}
}
Exclude Code Analysis Warnings
To exclude code defect warnings
For each of the remaining warnings, do the following:
In the Code Analysis window, select the warning.
Choose Actions, then choose Suppress Message, and then choose 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.