Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 21 Next »

The C# Ecosystem can be extended with custom coding rules either by:

  • defining them directly through the Sonar web interface using XPath expressions
  • or reusing custom ones defined in external tools such as FxCop, Gendarme or StyleCop

Extending Coding Rules using XPath

Migrating XPath rules from version 2.0 to 2.1+


Since version 2.1, grammar rule names are in uppercase, and each word in seperated by an underscore, such as: "COMPILATION_UNIT"
In version 2.0, camel cases was used, such as: "compilationUnit"
To convert XPath expressions written for version 2.0 to ones valid in 2.1, simply add underscores between words and uppercase.

New coding rules can be added using XPath. See the related documentation.

To navigate the AST, download the SSLR CSharp Toolkit.

Reusing Custom Rules from External Tools

You can reference your custom rules from FxCop and/or Gendarme and/or StyleCop in Sonar:

  1. Write a piece of XML to reference your custom coding rules. See formats below.
  2. Go to Settings > Configuration > General Settings > .Net FxCop or .Net Gendarme or C# StyleCop
  3. Paste this piece of XML in .NET/C# XXX custom rules
  4. Click on Save .NET/C# XXX settings
  5. Restart your Sonar server
  6. Activate your custom coding rules in your C# quality profiles

FxCop Format

RULE_CLASS: name of the .NET rule class
ASSEMBLY.DLL: name of the dll assembly file containing the rule class
The XML fragment above assumes that the dll is located in the same directory as the standard FxCop assembly rules files.

Note: to help writing this XML files, "vladonemo" has written a C# program that generates this file based on your custom rules DLL. You can check this out on GitHub, but be aware that this comes with no guarantee.


Let's assume that you have implemented a rule that checks whether a variable name follows a specific naming convention or not. You have implemented it in a class called "VariableConventionNameCheck", and you have compiled it into a "MyNamingConvetions.dll" assembly file. Then, you should write the following piece of XML:

Gendarme Format

RULE_CLASS: name of the .NET rule class
ASSEMBLY.DLL: name of the dll assembly file containing the rule class. This file should be located in the installation directory of Gendarme, among the dll files provided by Gendarme out of the box. See the example of FxCop XML format, it is the same principle.

StyleCop Format

  • No labels