Creating Custom FXCop rules and altering existing ones - Part 1

Recently I needed to create a set of custom FXCop rules and started playing with the latest version, which is currently 1.32. It quickly became apparent that things were not as easy as taking a quick look at the SDK, implementing an example, modifying to suit and away we go. For a start, most of the documentation still revolves around the old 'Reflection' engine, and the current version uses a new 'Introspection' engine. So that fact, and I imagine a couple of others, means the API has changed from previous versions. Recently, John Robbins wrote a bugslayer article on writing custom FXCop rules for version 1.30.

Things have changed slightly in the 1.32 release, however the article still proved quite useful. With a combination of that, and the trusty old 'Reflector' tool, I was able to come up with a decent, easy to use, re-useable project template that contains:

  • Simple example custom rules
  • Empty rules to use as a template for creating new rules
  • Easy to use abstract base class from which to base all custom rules from.
  • A correctly structured 'rules.xml' embedded resource file complete with example entries, and comments to aid you in creating new entries. This file contains the required <rule> node entry for each custom rule that in in the project.

The project is available here ( http://www.theglavs.com/GlavTech/Downloads/CustomFXCopRules_WhidbeyCode.zip ) and has been developed using the current Beta 2 of Visual Studio.NET (Build 50215.45). The code is easily ported to .Net V1.1 if so required (you should only need to change one class file that uses a static class 'Constants.cs'). All assembly references are included in the ZIP file.

If you are writing custom FXCop rules, then it will save you a considerable amount of time and make things super easy.

I also wanted to alter some of the Microsoft supplied existing rules. I know you can exclude them *once* you have perfomed an analysis run, but I wanted to completely disable/remove a rule at the outset. For that little story, you'll have to read my next post....

Update: I have created a .Net V1.1 / VS.NET 2003 version of the Custom FXCop rule template project mention above. It can be downloaded from here ( http://www.theglavs.com/GlavTech/Downloads/CustomFXCopRules_VS2003.zip )

10 Comments

  • Well there isn't one right now. Its pretty easy to convert, and I will try and do this over the next day or so. A little busy right now so if you can please bear with me ad ping me in a day or two if I haven't got back to you.

  • Hi Paul,

    I just wanted to say thanks for making this available. I have been wanting to get into making my own FxCop rulesets, and this is a great starter project.

    Robert

  • How do i add my custom code rule that i create to the pool of rules that already exist? I tried adding the file CustomFXCopRules.dll to the rules directory of FXCop.. When I start FXCop it says

    "Unable to load rule assembly 'c:\program files\microsoft fxcop 1.35\Rules\CustomFXCopRules.dll': Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information. "

    What do i do?

  • Daniel,
    Just sounds like you are referencing an assembly that FXCopy cannot find.

    Check what assemblies you are referencing and make sure they are all available.

  • Hello Daniel,

    How can I create custom rule for version1.35, can you give some URL or sample code.

    Regards,
    Kumar

  • Hi Melissa,

    I had the same problem and it got resolved by removing the FxCopSDK and Microsoft.cci and adding these references from the Prgramfiles/Microsoft FXcop folder.



  • Hello Daniel,

    How can I create custom rule for version1.35,I have never done it before so please give a step by step process. can you give some URL or sample code.

    Regards,

    Ashwin

  • Prashant,

    This just sounds like a basic method access issue. just make sure all the things are matching their signatures, all interfaces are present. Personally have never seent his problem before.

  • No luck. Even using the DLLs from the FXcop assembly for references, I get the "no rules" error from FXCop. Has anyone figured this out?

  • can u any one explain hw I modify an existing Rule in FxCop???

    Thanks
    Bijay
    http://www.fewlines4biju.com
    http://www.onlysharepoint2010.com

Comments have been disabled for this content.