How to Create Your StyleCop Rules

StyleCop is a free tool from Microsoft that allows you to check the style of your code (or others’). Additionally, StyleCop is now available as Open Source (on Codeplex), so I strongly recommend looking at the default rule implementations to help you create your own.

Create Your Rules

Here are some steps to create your custom StyleCop rules:

  • Install the latest version of StyleCop
  • Download the SDK of StyleCop (documentation .chm file)
  • Start Visual Studio
  • Create a new project of type “Class Library”
  • Add the following references - Microsoft.StyleCop - Microsoft.StyleCop.CSharp - Microsoft.StyleCop.CSharp.Rules

StyleCop_SolutionExplorer

Debugging

Since StyleCop rules are in its directory and StyleCop is integrated with Visual Studio, here are some configurations to help you (Change directories as needed):

  • In the project properties/Build Events: - Post-build event command line: xcopy “$(TargetDir)BHRules*.*” “C:\Program Files\Microsoft StyleCop 4.3.3.0” /y
  • In the project properties/Debug: - Start external program: C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe - Command line arguments (optional): Path to a solution you want to validate with your new rules.

If you have any questions, use the comments or the contact form.


See also