Skip to main content
Email Password   helpLost your password?

Introduction

FxCop

FxCop is a code analysis tool that checks managed code assemblies for conformance to Microsoft� .NET Framework design guidelines plus custom guidelines.

It uses introspection, MSIL parsing, and call graph analysis to inspect assemblies for more than 200 defects in the following areas: Library design, Localization, Naming conventions, Performance, Security. Introspection is described as a new faster, engine for performing Reflection on .NET classes .The main advantages of introspection engine are it performs a more extensive analysis than reflection, multithreading, analysis of assemblies of different versions of the framework, ability to Managed Extensions for C++ executables, and non-locking behaviour.

FxCop includes both GUI and command line versions of the tool. It also provides XML based test reports. You can write custom rules based on your company standards and add-up in FxCop tool to test your assembly.

Why FxCop?

Solution

Process

FxCop allows you to write / create your own rules or to use standards provided by Microsoft, and apply to your assembly. A rule is managed code that can analyze targets and return a message about its findings. FxCop analyses programming elements in managed assemblies and provide an informational report containing messages about the targets, including suggestions on how to improve the source code.

Coding standards: all developers are supposed to follow a company�s coding standards. It�s not an easy task to check manually the thousands of lines of code to ensure that coding standards are followed or not. FxCop solves this problem. You create an assembly having all these standards and run your assembly (DLL or EXE) against these rules, and FXCop will ensure that the specified rules are used in the coding or not.

Create a new project

Using the solution to solve the problem: Analyze the assemblies and review messages

Analyse the assemblies

Default rules of FXCop

Custom Rules

FxCop supports writing custom rules. The FxCop SDK is used to develop custom rules that extend the set of violations detected. Custom rules can be written by writing a class in any. NET language.

Steps to write Custom Rules in FxCop

  1. Create the project: Create a class library project that has the name of the DLL you want.
  2. Add the necessary references: You will need to add references to FxCopSdk.dll and Microsoft.Cci.dll.
  3. Create your first rule: Add a new class to the project to be used for implementing the rule. If you are not using any helping libraries, the class should be derived from BaseIntrospectionRule.
  4. Add the following using statements to the class:
    using Microsoft.Cci;
    using Microsoft.Tools.FxCop.Sdk;
    using Microsoft.Tools.FxCop.Sdk.Introspection;
  5. The constructor should look as follows:
    public class1() : 
        base( "class1",  // note that there is no "Rule"
    
              "ProjectName.RuleDefinitionsXML",
              // replace ProjectName with Company.FxCop.Category.
    
              typeof( class1 ).Assembly )
    {//TO DO}
  6. Create the RulesDefinitions.xml and add it to the project. Change the "Build Action" for this file to be "Embedded Resource".
  7. Implement the rule.
  8. Build the project.
  9. Add the rules to FxCop. There are two ways to make the rule visible in FxCop. The first is to add it to the rules\ sub-folder in FxCop, which will make it available to all FxCop projects. The other is to add it to a specific project by the "Project - Add Rules" menu option.

(Check the example for more information.)

Tech and Delivery method

One of the thumb rules is coding standards are around as long as the programming/development exists.

Please download the set-up file from here.

You can operate FxCop in three modes:

  1. In built with Visual studio 2005
  2. FxCop COM on Command prompts
  3. FxCop GUI

FxCop in Visual Studio 2005

FxCop Command

To specify the assemblies and rules, use one of the following:

To specify the location of the output file, use one of the following:

FXCop GUI

Key Components

Code Rules and standards, XML test reports, Free tool, Easy way for writing custom rules.

Financial Resources

It�s free. It also integrated with Visual Studio 2005.

Evaluation

Results and Benefits

FxCop works with compiled assemblies and uses metadata to get information about the code. It also has an extensive set of rules, which are based on the MS Coding practices for .NET.

FxCop advantages

A Similar tool to Microsoft FxCop is Code Analyzer, but it is a licensed product:

Code Analyzer:

Code Analyzer advantages (comparing to FxCop):

  1. FxCop is limited to assembly metadata; Code Analyzer works with source code and provides more functionality like comments, position in source code and more.
  2. FxCop has flat rules structure, which makes orientation in policy more difficult for larger policies. Code analyzer has hierarchical structure, based on logical rule categories.
  3. FxCop provides only one type of report, Code Analyzer is flexible and provides more report types and users can create their own report types.
Standards Master 2004:

Standards Master 2004 offers numerous advantages over FxCop. Many of these advantages may be credited to the fact that Standards Master works with your source code directly, not compiled assemblies as FxCop does. For example:

  1. When examining naming conventions, Standards Master can check all identifiers in your code, not just the ones that appear in META data. Generally, most variables are declared within method bodies, and these do not appear in the META data FxCop examines.
  2. Unlike FxCop, Standards Master 2004 can automatically fix many of the errors it finds. Since Standards Master works as an add-in, there is no need to switch tools to correct violations found. This means developers can fix standards violations easily while they work.
  3. The naming conventions features of Standards Master are completely configurable; those in FxCop are essentially fixed. Microsoft's naming guidelines are not universally accepted or used (especially in VB circles) and do not apply to private or local variables. If your own standards have varied from or extended Microsoft's suggestions in any way, FxCop will not accommodate your changes.
  4. Standards Master 2004 checks for dozens of code construction options that FxCop does not check.
  5. Standards Master 2004 offers robust commenting and formatting options that are completely unavailable in FxCop.

Lessons Learned

Sample Tests, Tables, etc.

Please check the attached zip project. In the example one custom rule is created to check all string variable should start with str as prefix. Install FxCop, and use the DLL file to verify your assembly.

More Info

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralNDepend Pin
Patrick Smacchia MVP.NET
13:51 6 May '07  
Questioncan't find reference components Pin
ebdrup
1:52 8 May '06  
GeneralFXCop shows how bad MS Designer code is Pin
tparker
8:30 29 Oct '05  
GeneralTools is not in the namespace anymore Pin
Jeff Lehmer
12:52 26 Oct '05  


Last Updated 17 Oct 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009