Writing Custom Rules in FxCop & FxCop Visual Studio Integration [Jeffrey van Gogh]

Two common questions we often get are:

  • Can I write my own rules for FxCop and customize existing rules?
  • Can I integrate FxCop into visual studio and can I jump to the location in my source of the item that FxCop flags as an error?

 That's why I want to give some more information on both:

FxCop supports writing custom rules. Custom Rules can be written by writing a class in any.NET language. This class has to implement one of the interfaces FxCop provides via its SDK. After compiling the class into an assembly, FxCop can load the custom rule by pointing it to that assembly. Samples and Documentation on how to do this can be found on our webpage:

the SDK Documentation: https://www.gotdotnet.com/team/fxcop/docs/gotdotnetstyle.aspx?url=UsingFxCopSdk.htm

the Samples: https://www.gotdotnet.com/team/fxcop/docs/SDKSamples.zip

FxCop doesn’t really support editing the default rules right now. We are making some rules configurable for our next release, e.g. you can change settings of the spelling rules and you can change on which members the Naming rules run (by default they run only on members visible outside of the assembly).

FxCop integrates into VS.NET as an external tool using the Commandline version of FxCop. On details how to do this, see https://www.gotdotnet.com/team/fxcop/docs/UsingFxCopCmd.htm#_Visual_Studio_Integration

FxCop is able to find sourcecode location for an error message if both following conditions are met:

  • The compiler emitted a .pdb file for the assembly (/debug+ switch for C# Compiler, enabled by default in VS)
  • The error message occurs in an executable piece of code inside the assembly (assembly, namespace, type, field & attribute error messages are not in executable code and the compiler therefore doesn’t generate source lookup information)

In the upcoming release of FxCop, with our new Metadata reader, we will be able to do a much better job for jumping to source code. Currently FxCop jumps to the first line of a method in which an FxCop issue is found. In the next version of FxCop we will jump to the exact line of code that has an issue.