Walkthrough: Analyzing Managed Code for Code Defects
In this walkthrough, you analyze a managed project for code defects 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 2005 Team System.
A copy of the ManagedDemo Sample.
To analyze a managed project for code defects
In Visual Studio 2005, open the ManagedDemo solution.
Select the ManagedDemo project in Solution Explorer.
On the Project menu, click Properties.
The ManagedDemo properties page is displayed.
Click Code Analysis.
Select Enable Code Analysis (defines CODE_ANALYSIS constant).
On the File menu, click Save Selected Items, and then close the ManagedDemo properties pages.
On the Build menu, click Build ManagedDemo.
The ManagedDemo project build warnings are reported in the Error List and Output windows.
To correct code analysis rule violations
On the View menu, click Error List.
Depending on the developer profile you chose, you might need to point to Other Windows on the View menu, and then click Error List.
In Solution Explorer, click Show All Files.
Next, expand the Configuration node, and then open the AssemblyInfo.cs file.
Use the following table to correct warnings:
Warnings | To correct warning |
---|---|
Assemblies should declare minimum security: Microsoft.Usage: No valid permission requests were found for assembly 'ManagedDemo'. You should always specify the minimum security permissions using SecurityAction.RequestMinimum. |
|
Mark assemblies with CLSCompliant: Microsoft.Design: 'ManagedDemo' should be marked with the CLSCompliantAttribute, and its value should be true. |
|
|
|
Mark ISerializable types with serializable: Microsoft.Usage: Add a [Serializable] attribute to type 'demo' as this type implements ISerializable. |
|
Implement standard exception constructors: Microsoft.Design: Add the following constructor to this class: public demo(String) |
|
Implement standard exception constructors: Microsoft.Design: Add the following constructor to this class: public demo(String, Exception) |
|
Implement standard exception constructors: Microsoft.Design: Add the following constructor to this class: protected demo(SerializationInfo, StreamingContext) |
|
Implement standard exception constructors: Microsoft.Design: Add the following constructor to this class: public demo() |
|
Identifiers should have correct suffix: Microsoft.Naming: Rename 'testCode.demo' to end in 'Exception'. |
|
Identifiers should be cased correctly: Microsoft.Naming: Correct the casing of namespace name 'testCode' |
|
Identifiers should be cased correctly: Microsoft.Naming: Correct the casing of member name 'item'. |
|
Assemblies should have valid strong names: Sign 'ManagedDemo' with a strong name key. |
|
Exclude Code Analysis Warnings
To exclude code defect warnings
Select the remaining warnings in the Error List, and then right-click and select Suppress Message(s).
Rebuild the project.
The project builds without any warnings or errors.