FxCop & StyleCop

What is FxCop ?

From the MSDN, FxCop is an IL code analyzer. It will analyze assemblies and check for rules violations according to the .NET Design Guidelines.

You can download it (v-1.36) from here. It is quite handy, even though not all rules make sense for all projects — especially if you are developing applications rather than libraries.

How to use it ?

Create a project and add compiled libraries for it to analyze. It is better to have it analyze assemblies with debug information so that it can link to the sources.

You can then decide the categories of rules to run and which to exclude. The next step is to analyze your assemblies which will provide a report containing details of all rule violations. This can be a pretty heavy report, especially if you have a large project already advanced in its life cycle.

It is now time to take care of the violations found, either by fixing them, excluding them from the project or excluding them from the source. Sometimes it is not possible or it does not make sense to fix a violation (according to the context) so it is preferable to exclude it so that it does not keep coming back. Excluding them from the project is quick but prone to later problem: conflicts in or loss of the project file, renaming of files/classes/methods. Those problems disappear when using in source exclusion with the SuppressMessageAttribute specifying the rule violation to suppress and possibly a justification for future reference. Here is an example (I always put my constant in ALL CAPS):

   21 [SuppressMessage( "Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly",
   22                   MessageId = "WIDTH", Justification="Coding conventions" )]

FxCop is integrated in the team edition of Visual Studio but not in the free or professional edition.

In order to get this attribute to work, the CODE_ANALYSIS symbol must be defined during the compilation of the assembly. Without this, FxCop will ignore the violations exclusions defined in the source code.

For batch run (or MS Build integration), you can use the FxCopCmd utility. You can find here a tutorial on how to integrate FxCopCmd with MS Build.


StyleCop is a closely related tool as it does source code analysis but directly on C# code rather than on disassembled MS IL. It also provides a set of rules and can be run from within Visual Studio.

It can be downloaded (latest version) from here. You can find here a tutorial on how to integrate StyleCop within an MS Build project.

This entry was posted in .NET, Software and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s