StyleCop analyzes C# code with a set of rules to check whether the code follows the rules. You can download StyleCop from this link. If you click on the downloads section, you will see three setups, one is the StyleCop setup which actually installs the software and the other two are CHM help files. One help file talks about the rules and the other about the API exposed by StyleCop.
Once you install StyleCop, nothing will be seen as in start menu. When you open any project of C# and right click on the project, you should see two menus in the project as shown in the figure below - one which helps us select the rules, i.e. the Stylecop settings menu and the other which runs these rules on the C# Project.

If you just run the stylecop, you should see broken rules in output window as shown in the figure below:

Comparing FXCOP with StyleCop
|
FXCOP |
StyleCop |
|
It runs on compiled DLLs. |
It runs on actual source code. |
|
As it runs on compiled IL code it can be used for C#, VB.NET, in short any language which compiles to IL code. |
Currently it runs only on C#. |
Currently, I can
Currently, I can think about two differences. If any one knows more, let me know so that I can update this. Many people are asking for a comparison table for these two codes review tools.
Issue of Code Review using FXCOP
First, let me thank Thomas Wellar for giving me accurate information about FXCOP issues with code review.
FXCOP works on the actual compiled DLL. For instance, a switch method shown below can be compiled to dictionary of string by the .NET compiler. Due to this, we cannot actually measure what kind of quality the code has. The conversion from actual source code to IL is very much non-linear. This is where StyleCop has an extra edge as it operates directly on the source code. The only negative point it has currently is that it only works for C#.

StyleCop Code Parsing Logic
StyleCop has two important concepts - one is the document and the other the elements. Document is the file and elements are everything inside the file.

For instance, below is a simple source code which has two methods, properties and variables
So stylecop treats everything inside the document as an element. Now let's understand how stylecop browses through the document by implementing a custom rule in stylecop.
Making the Custom Rules Class
The first thing is to import stylecop namespaces. Please ensure that you give references from c:\Program files\Microsoft Style Cop folder.
|
All stylecop custom rule classes should inherit from SourceAnalyzer class:
|
When the style cop starts running, it first hits the analyzedocument method. So override the method and let's put our logic in this:
|
In the analyze document, we type cast the document into CsDocument class. As we had previously said, stylecop walks through the document and then walks through each element. This it achieves by using delegates and visitor pattern. The WalkDocument method visits the VisitElement for every element in the source code. In case you are curious about visitor pattern, you can always read the same from this article.
|




Former memberPosted Aug 4, 2014, 9:55 AM
Nice one
Dheeraj RamakrishnaPosted May 27, 2009, 6:50 AM
I don't see how we can set design rules in this tool. FxCop can perform design analysis for predefined rules. This tool more or less helps in cleaning up the code not from a desgn perspective.