Поделиться через


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

  1. On the File menu of Visual Studio, click New and then click Project.

  2. In the New Project dialog box, under Project Types, click Visual C#.

  3. Under Templates, select Class Library.

  4. In the Name text box, type CodeAnalysisManagedDemo and then click OK.

  5. After the project is created, open the Class1.cs file.

  6. Replace the existing text in Class1.cs with the following code:

    //CodeAnalysisManagedDemo
    //Class1.cs
    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; } }
        }
    }
  7. Save the Class1.cs file.

Analyze the project

To analyze a managed project for code defects

  1. Select the CodeAnalysisManagedDemo project in Solution Explorer.

  2. On the Project menu, click Properties.

    The CodeAnalysisManagedDemo properties page is displayed.

  3. Click Code Analysis.

  4. Select Enable Code Analysis on Build (defines CODE_ANALYSIS constant).

  5. From the Run this rule set drop-down list, select Microsoft All Rules.

  6. On the File menu, click Save Selected Items, and then close the ManagedDemo properties pages.

  7. 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

  1. 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.

  2. In Solution Explorer, click Show All Files.

  3. Next, expand the Properties node, and then open the AssemblyInfo.cs file.

  4. 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.

  1. Add the code using System; to the AssemblyInfo.cs file.

  2. Next, add the code [assembly: CLSCompliant(true)] to the end of the AssemblyInfo.cs file.

  3. Rebuild the project.

CA1032: Implement standard exception constructors: Microsoft.Design: Add the following constructor to this class: public demo(String)

  • Add the constructor public demo (String s) : base(s) { } to the class demo.

CA1032: Implement standard exception constructors: Microsoft.Design: Add the following constructor to this class: public demo(String, Exception)

  • Add the constructor public demo (String s, Exception e) : base(s, e) { } to the class demo.

CA1032: Implement standard exception constructors: Microsoft.Design: Add the following constructor to this class: protected demo(SerializationInfo, StreamingContext)

  1. Add the code using System.Runtime.Serialization; to the beginning of the Class1.cs file.

  2. Next, add the constructor protected demo (SerializationInfo info, StreamingContext context) : base(info, context) { } to the class demo.

  3. Rebuild the project.

CA1032: Implement standard exception constructors: Microsoft.Design: Add the following constructor to this class: public demo()

  1. Add the constructor public demo () : base() { } to the class demo.

  2. Rebuild the project.

CA1709: Identifiers should be cased correctly: Microsoft.Naming: Correct the casing of namespace name 'testCode' by changing it to 'TestCode'.

  • Change the casing of the namespace testCode to TestCode.

CA1709: Identifiers should be cased correctly: Microsoft.Naming: Correct the casing of type name 'demo' by changing it to 'Demo'.

  • Change the name of the member to Demo.

CA1709: Identifiers should be cased correctly: Microsoft.Naming: Correct the casing of member name 'item' by changing it to 'Item'.

  • Change the name of the member to Item.

CA1710: Identifiers should have correct suffix: Microsoft.Naming: Rename 'testCode.demo' to end in 'Exception'.

  • Change the name of the class and its constructors to DemoException.

CA2210: Assemblies should have valid strong names: Sign 'ManagedDemo' with a strong name key.

  1. On the Project menu, click ManagedDemo Properties.

    The project properties appear.

  2. Click Signing.

  3. Select the Sign the assembly check box.

  4. In the Choose a string name key file list, select <New…>.

    The Create Strong Name Key dialog box appears.

  5. In the Key file name, type TestKey.

  6. Enter a password and then click OK.

  7. On the File menu, click Save Selected Items, and then close the property pages.

  8. Rebuild the project.

CA2237: Mark ISerializable types with SerializableAttribute: Microsoft.Usage: Add a [Serializable] attribute to type 'demo' as this type implements ISerializable.

  1. Add the [Serializable ()] attribute to the class demo.

  2. Rebuild the project.

Exclude Code Analysis Warnings

To exclude code defect warnings

  1. 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

  2. Rebuild the project.

    The project builds without any warnings or errors.