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 )