Creating Custom FXCop rules and altering existing ones - Part 1

Published Tuesday, July 19, 2005 6:57 PM

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 )

by Glav

Comments

# Jose Cyriac said on Monday, August 01, 2005 5:19 AM

Hi, From where I acn get the Visual Studio.NET 2003 version of http://www.theglavs.com/GlavTech/Downloads/CustomFXCopRules_WhidbeyCode.zip...
Please givea suggestion

# Paul Glavich said on Monday, August 01, 2005 6:11 AM

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.

# Paul Glavich said on Monday, August 01, 2005 10:52 PM

Jose,

I have created a .Net V1.1/VS2003 version for you. It can be downloaded from the same page as the whidbey version. Here is the direct link http://www.theglavs.com/GlavTech/Downloads/CustomFXCopRules_VS2003.zip

# Robert Hencke said on Tuesday, June 05, 2007 12:41 AM

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

# Daniel said on Tuesday, September 04, 2007 11:19 AM

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?

# Glav said on Sunday, September 09, 2007 7:32 AM

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.

# Kumar said on Friday, September 14, 2007 2:51 AM

Hello Daniel,

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

Regards,

Kumar

# Melissa said on Friday, October 05, 2007 8:55 AM

Hi Daniel,

I have developed custom rules using Introspection (recognized by FxCop 1.35) and the project was built without any errors. I am using FxCop 1.35. When I try to add the custom rules dll to the rules tab it gives me an error: "Unable to load rule assembly 'C:\Documents and Settings\[user]\My Documents\Visual Studio 2005\Projects\FxCopCustomRules\FxCopCustomRules\bin\Debug\FxCopCustomRules.dll': The assembly contains no FxCop rules." It shows the source as "FxCopCommon" What could be causing this error?

Appreciate your help. Thanks in advance.

# Rama Kanth said on Thursday, November 22, 2007 4:41 AM

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.

# Ashwin said on Thursday, December 06, 2007 6:48 AM

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 said on Saturday, January 05, 2008 9:50 AM

I am getting the error "Unable to load rule assembly 'c:\program files\microsoft fxcop 1.35\Rules\FxCopSdk.dll': The assembly contains no FxCop rules."

As suggested I tried to change the reference of  FxCopSDK and Microsoft.cci from "Program Files\Microsoft FxCop 1.35" then I got new error saying "This not a valid assembly or COM component"

I Appreciate your help. Thanks in advance.

# Glav said on Sunday, January 06, 2008 5:34 AM

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.

# Duane Roelands said on Wednesday, September 24, 2008 1:39 PM

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?

# bijaya1981 said on Friday, June 17, 2011 5:11 AM

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

Thanks

Bijay

http://www.fewlines4biju.com

www.onlysharepoint2010.com

# Prevent Legacy Function Calls In VB.NET - Programmers Goodies said on Friday, July 08, 2011 8:56 PM

Pingback from  Prevent Legacy Function Calls In VB.NET - Programmers Goodies

Leave a Comment

(required) 
(required) 
(optional)
(required) 

This Blog

Syndication