Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

How to use a custom rule

Just as the xml rules engines, the C# rules engines The C# Ecosystem can be extended with custom rules either by:

  • defining your own rules directly through the Sonar web interface using XPath expressions
  • or by reusing custom rules defined in external tools

Extending Coding Rules using XPath (since version 2.

...

0)

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

Once you have packaged your custom rule in a dll file and added it to your FxCop/Gendarme installation, you need to add an XML file describing this rule in the "$SONAR_HOME/extensions/rules/ENGINE" where $SONAR_HOME is the root directory of your sonar installation and ENGINE is either "fxcop" or "gendarme".
Below details on the format of these files.

FxCop XML

...

Format

Code Block
<rules>
  <rule key="RULE_CLASS">
    <name><![CDATA[ ... put here the human readable name of this rule ... ]]></name>
    <configKey><![CDATA[RULE_CLASS@$(FxCopDir)\Rules\ASSEMBLY.DLL]]></configKey>
    <category name="Maintainability" />
    <description><![CDATA[ ... put here the human readable description of this rule ... ]]></description>
  </rule>
</rules>

RULE_CLASS being the name of the .net rule class.
ASSEMBLY.DLL being the 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.

Example

Let's assume you have implemented a rule that checks if variable name follows a naming convention. You have implemented it in a class called "VariableConventionNameCheck", and you have compiled it into a "MyNamingConvetions.dll" assembly file.

...

Code Block
<rules>
  <rule key="VariableConventionNameCheck">
    <name>Variable name conventions</name>
    <configKey><![CDATA[VariableConventionNameCheck@$(FxCopDir)\Rules\MyNamingConvetions.dll]]></configKey>
    <category name="Maintainability" />
    <description><![CDATA[ This rule checks that variables follow the company naming conventions ]]></description>
  </rule>
</rules>

Gendarme XML

...

Format

Code Block
<rules>
  <rule key="RULE_CLASS">
    <name><![CDATA[ ... put here the human readable name of this rule ... ]]></name>
    <configKey><![CDATA[RULE_CLASS@ASSEMBLY.DLL]]></configKey>
    <category name="Maintainability" />
    <description><![CDATA[ ... put here the human readable description of this rule ... ]]></description>
  </rule>
</rules>

...

See the example of FxCop XML format, this is the same principle.

StyleCop XML

...

Format

Example:

Code Block
<rules>
  <rule key="ElementMustBeginWithUpperCaseLetter">
    <name><![CDATA[Element must begin with upper case letter]]></name>
    <configKey><![CDATA[Microsoft.StyleCop.CSharp.NamingRules#ElementMustBeginWithUpperCaseLetter]]></configKey>
    <category name="Usability" />
    <description><![CDATA[Element must begin with upper case letter because ....]]></description>
  </rule>
</rules>
Note
titleComing in 2.0

Extending Coding Rules using XPath (since 2.0)

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

To navigate the AST, download the SSLR CSharp Toolkit.