Oct 20

FxCop: removing rules

Posted in C# Tools      Comments Off on FxCop: removing rules

One of the things I’ve been using lately is FxCop. It’s really an interesting tool and I’ve corrected several API problems on my classes after starting using it. In fact, I’ve only found two problems until now:

  1. Interface explicit implementation
  2. Incorrect spelling of names

Ok, let’s start by problem number 1. Here’s the code I’m using:

public class Something: ISomething{
  void ISomething.DoSomething(){
  protected virtual void DoSomething(){

When FxCop sees this rule, it will automatically report the InterfaceMethodsShouldBeCallableByChildTypes error. The error message says that something like this:

"Explicit method implementations are defined with private accessibility. Classes that derive from classes with explicit method implementations and choose to re-declare them on the class will not be able to call into the  base class implementation unless the base class has provided an alternate method with appropriate accessibility. “

Most of the time, I do end up using public implementation method, but sometimes I only want to allow access to that method through the interface. That’s what I’m trying to do in the previous example. Now, if you look at the code, you’ll see that a derived class can customize the behavior of the method by overriding the DoSomething protected method. So, this is one error I don’t like and I tend to classify it as a false positive.

The second problem is even funnier: I’m getting the IdentifiersShouldBeSpelledCorrectly and CompoundWordsShouldBeCasedCorrectly errors. After somme googling, I’ve managed to understand that FxCop ends up using the Office dictionary for checking the words you use in your member names. Well, it happens that my server doesn’t have office installed, so it starts giving me lots and lots of false positives. Looking at the docs,you’ll see that you can add an xml file for adding the allowed and disallowed words.

Since I’m using the command line tool (fxcopcmd),I thought that there should be a way to stop processing some rules. After looking on the docs, I found nothing. So, I’ve decided running the fxcopcmd executable and I’ve found that there is a rid parameter that lets you add or remove rules. So, if you want to remove the annoying spelling rule, you can run something like this:

fxcopcmd –f:myassembly.dll –rid:-”Microsoft.Naming#CA1033” /console

And now you should stop seeing those spelling warnings all over the place.