Export (0) Print
Expand All
5 out of 5 rated this helpful - Rate this topic

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.

To analyze a managed project for code defects

  1. In Visual Studio 2005, open the ManagedDemo solution.

  2. Select the ManagedDemo project in Solution Explorer.

  3. On the Project menu, click Properties.

    The ManagedDemo properties page is displayed.

  4. Click Code Analysis.

  5. Select Enable Code Analysis (defines CODE_ANALYSIS constant).

  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 ManagedDemo project build warnings are reported in the Error List and Output windows.

To correct code analysis rule violations

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

  2. In Solution Explorer, click Show All Files.

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

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

  1. Add the code using System.Security.Permissions; to the using section of the AssemblyInfo.cs file.

  2. Next, add the code [assembly:SecurityPermission(SecurityAction.RequestMinimum, UnmanagedCode=false)] to the end of the AssemblyInfo.cs file.

Mark assemblies with CLSCompliant: Microsoft.Design: 'ManagedDemo' 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.

Compiler Warning (level 1) CS3008.

  1. Change the name of the field _item to ItemCount.

  2. Modify public static int item { get { return _item; } } to return ItemCount.

  3. Rebuild the project.

Mark ISerializable types with serializable: 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.

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.

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.

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.

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.

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.

Identifiers should be cased correctly: Microsoft.Naming: Correct the casing of namespace name 'testCode'

  • Change the casing of the namespace testCode to TestCode.

Identifiers should be cased correctly: Microsoft.Naming: Correct the casing of member name 'item'.

  • Change the name of the member to Item.

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.

To exclude code defect warnings

  1. Select the remaining warnings in the Error List, and then right-click and select Suppress Message(s).

  2. Rebuild the project.

    The project builds without any warnings or errors.

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.