Walkthrough: Use static code analysis to find code defects
In this walkthrough, you'll analyze a managed project for code defects by using legacy code analysis.
This article steps you through the process of using legacy analysis to analyze your .NET managed code assemblies for conformance with the .NET design guidelines.
Create a class library
Open Visual Studio and create a new project from the Class Library (.NET Framework) template.
Name the project CodeAnalysisManagedDemo.
After the project is created, open the Class1.cs file.
Replace the existing text in Class1.cs with the following code:
using System; namespace testCode { public class demo : Exception { public static void Initialize(int size) { } protected static readonly int _item; public static int item { get { return _item; } } } }
Save the Class1.cs file.
Analyze the project for code defects
Select the CodeAnalysisManagedDemo project in Solution Explorer.
On the Project menu, click Properties.
The CodeAnalysisManagedDemo properties page is displayed.
Choose the Code Analysis tab.
Make sure that Run on build is selected in the Binary analyzers section.
From the Active rules dropdown list, select Microsoft All Rules.
On the File menu, click Save Selected Items, and then close the properties pages.
On the Build menu, click Build CodeAnalysisManagedDemo.
The CodeAnalysisManagedDemo project build warnings are shown in the Error List and Output windows.
Correct the code analysis issues
On the View menu, choose Error List.
Depending on the developer profile that you chose, you might have to point to Other Windows on the View menu, and then choose Error List.
In Solution Explorer, choose Show All Files.
Expand the Properties node, and then open the AssemblyInfo.cs file.
Use the following tips to correct the warnings:
CA1014: Mark assemblies with CLSCompliantAttribute: Add the code
[assembly: CLSCompliant(true)]
to the end of the AssemblyInfo.cs file.CA1032: Implement standard exception constructors: Add the constructor
public demo (String s) : base(s) { }
to the classdemo
.CA1032: Implement standard exception constructors: Add the constructor
public demo (String s, Exception e) : base(s, e) { }
to the classdemo
.CA1032: Implement standard exception constructors: Add the constructor
protected demo (SerializationInfo info, StreamingContext context) : base(info, context) { }
to the class demo. You'll also need to add ausing
statement for System.Runtime.Serialization.CA1032: Implement standard exception constructors: Add the constructor
public demo () : base() { }
to the classdemo
.CA1709: Identifiers should be cased correctly: Change the casing of the namespace
testCode
toTestCode
.CA1709: Identifiers should be cased correctly: Change the name of the member to
Demo
.CA1709: Identifiers should be cased correctly: Change the name of the member to
Item
.CA1710: Identifiers should have correct suffix: Change the name of the class and its constructors to
DemoException
.CA2237: Mark ISerializable types with SerializableAttribute: Add the
[Serializable ()]
attribute to the classdemo
.CA2210: Assemblies should have valid strong names: Sign 'CodeAnalysisManagedDemo' with a strong name key:
On the Project menu, choose CodeAnalysisManagedDemo Properties.
The project properties appear.
Choose the Signing tab.
Select the Sign the assembly checkbox.
In the Choose a string name key file list, select <New>.
The Create Strong Name Key dialog box appears.
For Key file name, enter TestKey.
Enter a password, and then choose OK.
On the File menu, choose Save Selected Items, and then close the property pages.
After you complete all the changes, the Class1.cs file should look like the following:
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; } } } }
Rebuild the project.
Exclude code analysis warnings
For each of the remaining warnings, do the following:
Select the warning in the Error List.
From the right-click menu (context menu), choose Suppress > In Suppression File.
Rebuild the project.
The project builds without any warnings or errors.