New users as well as users of the recent versions of the C# plugins should consult this page.

 

 

 

 

 

 

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

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.

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

<?xml version="1.0" encoding="UTF-8"?><rules>

  <rule key="RULE_CLASS">
    <!-- name, configKey and description are mandatory -->
    <name><![CDATA[ ... put here the human readable name of this rule ... ]]></name>
    <configKey><![CDATA[RULE_CLASS@$(FxCopDir)\Rules\ASSEMBLY.DLL]]></configKey>
    <description><![CDATA[ ... put here the human readable description of this rule ... ]]></description>
  </rule>

  <rule key="... >
    ...
  </rule>

</rules>

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:

<?xml version="1.0" encoding="UTF-8"?>
<rules>
  <rule key="VariableConventionNameCheck">
    <!-- name, configKey and description are mandatory -->
    <name>Variable name conventions</name>
    <configKey><![CDATA[VariableConventionNameCheck@$(FxCopDir)\Rules\MyNamingConvetions.dll]]></configKey>
    <description><![CDATA[ This rule checks that variables follow the company naming conventions ]]></description>
  </rule>
</rules>

Gendarme Format

<?xml version="1.0" encoding="UTF-8"?>
<rules>

  <rule key="RULE_CLASS">
    <!-- name, configKey and description are mandatory -->
    <name><![CDATA[ ... put here the human readable name of this rule ... ]]></name>
    <configKey><![CDATA[RULE_CLASS@ASSEMBLY.DLL]]></configKey>
    <description><![CDATA[ ... put here the human readable description of this rule ... ]]></description>
  </rule>

  <rule key="... >
    ...
  </rule>

</rules> 

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

<?xml version="1.0" encoding="UTF-8"?>
<rules>

  <rule key="ElementMustBeginWithUpperCaseLetter">
    <!-- name, configKey and description are mandatory -->
    <name><![CDATA[Element must begin with upper case letter]]></name>
    <configKey><![CDATA[Microsoft.StyleCop.CSharp.NamingRules#ElementMustBeginWithUpperCaseLetter]]></configKey>
    <description><![CDATA[Element must begin with upper case letter because ....]]></description>
  </rule>

  <rule key="... >
    ...
  </rule>

</rules>