C#静态代码检查工具StyleCode -- 自定义规则

转载自:StyleCop学习笔记——自定义规则


本文将简单的一步一步的指导这可能有助于学习如何创建自己的规则
1、创建一个项目。

Visual Studio创建一个新的类库项目.NET3.5

2、引用两个DLL,StyleCop.dll和StyleCop.Csharp.dll.


3、添加自定义的规则。


MyCustomAnalyzer.cs代码如下:

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. using StyleCop;  
  2. using StyleCop.CSharp;  
  3.   
  4. namespace MyCustomRules  
  5. {  
  6.     /// <summary>  
  7.     /// Custom analyzer for demo purposes.  
  8.     /// </summary>  
  9.     [SourceAnalyzer(typeof(CsParser))]  
  10.     public class MyCustomAnalyzer : SourceAnalyzer  
  11.     {  
  12.         /// <summary>  
  13.         /// Extremely simple analyzer for demo purposes.  
  14.         /// </summary>  
  15.         public override void AnalyzeDocument(CodeDocument document)  
  16.         {  
  17.             CsDocument doc = (CsDocument)document;  
  18.   
  19.             // skipping wrong or auto-generated documents  
  20.             if (doc.RootElement == null || doc.RootElement.Generated)  
  21.                 return;  
  22.   
  23.             // check all class entries  
  24.             doc.WalkDocument(CheckClasses);  
  25.         }  
  26.   
  27.         /// <summary>  
  28.         /// Checks whether specified element conforms custom rule CR0001.  
  29.         /// </summary>  
  30.         private bool CheckClasses(  
  31.             CsElement element,  
  32.             CsElement parentElement,  
  33.             object context)  
  34.         {  
  35.             // if current element is not a class then continue walking  
  36.             if (element.ElementType != ElementType.Class)  
  37.                 return true;  
  38.   
  39.             // check whether class name contains "a" letter  
  40.             Class classElement = (Class)element;  
  41.             if (classElement.Declaration.Name.Contains("a"))  
  42.             {  
  43.                 // add violation  
  44.                 // (note how custom message arguments could be used)  
  45.                 AddViolation(  
  46.                     classElement,  
  47.                     classElement.Location,  
  48.                     "AvoidUsingAInClassNames",  
  49.                     classElement.FriendlyTypeText);  
  50.             }  
  51.   
  52.             // continue walking in order to find all classes in file  
  53.             return true;  
  54.         }  
  55.     }  
  56.   
  57. }  

4、添加一个规则的XML文件,命名和上面类的名字一样。

把以下内容写到MyCustomAnalyzer.xml文件中

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <SourceAnalyzer Name="My Custom Rule">  
  3.     <Description>  
  4.         Custom rule for demo purposes.  
  5.     </Description>  
  6.     <Rules>  
  7.         <Rule Name="AvoidUsingAInClassNames" CheckId="CR0001">  
  8.             <Context>不能用A字母</Context>  
  9.             <Description>Fires when 'a' letter is used in class name.</Description>  
  10.         </Rule>  
  11.     </Rules>  
  12. </SourceAnalyzer>  

5、构建

将这个项目生成DLL,把MyCustomAnalyzer.dll放到StyleCop根目录下。


6、部署

打开一个我们要测试的项目代码。点击StyleCop Setting设置用我们的MyCoustomRule。


7、点击RunStyleCop在错误警告列表就会显示检测出来的规则验证。如图:


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值