Skip to end of metadata
Go to start of metadata

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

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

Extending Coding Rules using XPath

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

To navigate the AST, download the SSLR CSharp Toolkit.

Migrating XPath rules from version 2.0 to 2.1+

Icon

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.

Reusing Custom Rules from External Tools

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

  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 your XML in .NET/C# XXX custom rules
  4. Click on Save .NET/C# XXX settings
  5. Restart your SonarQube 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 write this XML file, "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 it comes with no guarantee.

Example

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