This article explains to create custom rules in stylecop with easy steps. In general, stylecop is used to identify issues in the written code (Technical) and refactor the code.
Step 1 - Creation of Project:
Create a new Visual Studio class library project targeting .NET 3.5/4.0.
Step 2 – Adding StyleCop Reference:
· StyleCop
· StyleCop.CSharp
Step 3 – Add Custom Analyzer Class:
Once the references are added, add your custom class for Stylecop analyzer. There can be many analyzer classes inside one assembly.
“Copy and Paste” the below code in your analyzer class.
namespace StyleCopNewRule
{
using StyleCop;
using StyleCop.CSharp;
[SourceAnalyzer(typeof(CsParser))]
public class MyOwnCustomAnalyzer : SourceAnalyzer
{
public override void AnalyzeDocument(CodeDocument currentCodeDocument)
{
var codeDocument = (CsDocument)currentCodeDocument;
if (codeDocument.RootElement != null && !codeDocument.RootElement.Generated)
{
codeDocument.WalkDocument(new CodeWalkerElementVisitor<object>(this.InspectCurrentElement), null, null);
}
}
private bool InspectCurrentElement(CsElement element, CsElement parentElement, object context)
{
if (element.ElementType == ElementType.Method)
{
bool boolIsTryFound = false;
bool boolIsCatchFound = false;
var tempDocument = (CsDocument)element.Document;
var objReader = tempDocument.SourceCode.Read();
string strCode;
while ((strCode = objReader.ReadLine()) != null)
{
if (strCode.Contains("try"))
{
boolIsTryFound = true;
}
if (boolIsTryFound)
{
if (strCode.Contains("catch"))
{
boolIsCatchFound = true;
}
}
}
if ((boolIsTryFound) && (boolIsCatchFound == false))
{
this.AddViolation(parentElement, "MyOwnCustomRule", "CatchShouldBeImplemented");
}
}
return true;
}
}
}
Step 4 - Add analyzer definition XML file:
Rule definition should be present for each analyzer file. Create XML file as mentioned below
· It should have same name given for analyzer file.
· Select “Build Action” as “Embedded Resource”
“Copy and Paste” the below content in the XML file:
<?xml version="1.0" encoding="utf-8" ?>
<SourceAnalyzer Name="My Own Custom Rule">
<Description> Custom rules added to analyzer. </Description>
<Rules>
<RuleGroup Name="Implementation">
<Rule Name="MyOwnCustomRule" CheckId="HE2222">
<Context>Catch block Should be present.</Context>
<Description>Catch block Should be present.</Description>
</Rule>
</RuleGroup>
</Rules>
</SourceAnalyzer>
Step 5 - Build and Deployment:
Build the library, and place the output file inside the StyleCop installation directory.
Step 6 - View the Custom File Settings:
Reopen your solution and you can see the custom file has been loaded in the “StyleCop Settings” Editor.
Step 7: Check Custom Style Cop settings Works!
Create a method use “try” without using “Catch” block. The stylecop exception will be displayed as warning. The warning will have the information about your custom rule.
This is an example on how to create own custom class in stylecop. You can write your own rules to align with your coding practice. Happy Learning! |