Writing your own rules for Microsoft Source Analysis for C#
Marcelo announced the release of the Microsoft Source Analysis for C# over at MSDN Code Gallery. Quoting from the newly formed blog specially dedicated to that tool:
This tool is known internally within Microsoft as StyleCop, and has been used for many years now to help teams enforce a common set of best practices for layout, readability, maintainability, and documentation of C# source code.
I am always looking for a way to improve my coding style and I was hoping to find out more about what the Microsoft coding style looks like. And I was fortunate: The tool actually ships with a lot of baked in rules … which I would be happy to comply to, if I could just find out how to comply to them.
So I set out to find out how that tool works – and how to tweak and adjust the build in sets. Hint: Installation directory is “%ProgramFiles%/Microsoft Source Analysis Tool for C#”.
What the tool basically does is:
- find all files in the current solution
- Create a parse tree of any given file
- load all dlls in installation folder and find all classes derived from Microsoft.SourceAnalysis.SourceAnalyzer
- Execute each analyzer
- Report
To find out more about the rules that ship with Source Analysis (loaded in step 3) on has to look at the assembly Microsoft.SourceAnalysis.CSharp.Rules.dll.
The ones baked in are specifically:
- DocumentationRules
- LayoutRules
- MaintainabilityRules
- NamingRules
- OrderingRules
- ReadabilityRules
- SpacingRules
And if you want to replace them with your own custom set, all you have to do is delete that particular dll – and design your own set. The code is very well structured, so it should not too hard to find your way around using Reflector. Enjoy.

[...] Writing Your Own Rules for Microsoft Source Analysis for C# (Tobias Hertkorn) [...]
Pingback on May 26, 2008 @ 15:58:18
If you’re interested, I’ve started some blog entries on creating custom rules to extend StyleCop / Source Analyzer: Love the Dot
Comment on May 28, 2008 @ 20:14:20
Can you tell how can I totally turn off DocumentaionRules (not for each project but for Analysis globally)?
Comment on May 29, 2008 @ 11:55:42
@Paul: Thanks for the tip.
@Ilya: Sorry, I don’t think I can help you there. What you could do is disassemble the assembly, remove the rules you want to disable permanently – and recompile.
Comment on June 5, 2008 @ 15:48:37