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

1、创建一个项目。

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

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

3、添加自定义的规则。

MyCustomAnalyzer.cs代码如下:

using StyleCop;
using StyleCop.CSharp; namespace MyCustomRules
{
/// <summary>
/// Custom analyzer for demo purposes.
/// </summary>
[SourceAnalyzer(typeof(CsParser))]
public class MyCustomAnalyzer : SourceAnalyzer
{
/// <summary>
/// Extremely simple analyzer for demo purposes.
/// </summary>
public override void AnalyzeDocument(CodeDocument document)
{
CsDocument doc = (CsDocument)document; // skipping wrong or auto-generated documents
if (doc.RootElement == null || doc.RootElement.Generated)
return; // check all class entries
doc.WalkDocument(CheckClasses);
} /// <summary>
/// Checks whether specified element conforms custom rule CR0001.
/// </summary>
private bool CheckClasses(
CsElement element,
CsElement parentElement,
object context)
{
// if current element is not a class then continue walking
if (element.ElementType != ElementType.Class)
return true; // check whether class name contains "a" letter
Class classElement = (Class)element;
if (classElement.Declaration.Name.Contains("a"))
{
// add violation
// (note how custom message arguments could be used)
AddViolation(
classElement,
classElement.Location,
"AvoidUsingAInClassNames",
classElement.FriendlyTypeText);
} // continue walking in order to find all classes in file
return true;
}
} }

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

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

<?xml version="1.0" encoding="utf-8" ?>
<SourceAnalyzer Name="My Custom Rule">
<Description>
Custom rule for demo purposes.
</Description>
<Rules>
<Rule Name="AvoidUsingAInClassNames" CheckId="CR0001">
<Context>不能用A字母</Context>
<Description>Fires when 'a' letter is used in class name.</Description>
</Rule>
</Rules>
</SourceAnalyzer>

5、构建

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

6、部署

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

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

StyleCop学习笔记——自定义规则的更多相关文章

  1. iOS学习笔记-自定义过渡动画

    代码地址如下:http://www.demodashi.com/demo/11678.html 这篇笔记翻译自raywenderlick网站的过渡动画的一篇文章,原文用的swift,由于考虑到swif ...

  2. StyleCop学习笔记——默认的规则

    在StyleCop中有一些官方自己写好的检测规则下面就是英文的解释 文档规则 1.SA1600:ElementsMustBeDocumented元素必须添加注释 2.SA1601: PartialEl ...

  3. StyleCop学习笔记-文档规则

    文档规则: .SA1600:ElementsMustBeDocumented元素必须添加注释 .SA1601: PartialElementsMustBeDocumented Partial修饰的成员 ...

  4. Angular JS 学习笔记(自定义服务:factory,Promise 模式异步请求查询:$http,过滤器用法filter,指令:directive)

    刚学没多久,作了一个小项目APP,微信企业号开发与微信服务号的开发,使用的是AngularJS开发,目前项目1.0版本已经完结,但是项目纯粹为了赶工,并没有发挥AngularJS的最大作用,这几天项目 ...

  5. #Linux学习笔记# 自定义shell终端提示符

    我使用的Linux发行版是LinuxMint 17.2 Rafaela,默认情况下Terminal中的shell提示包括了用户名.主机名.当前目录(绝对路径)和提示符.这样会导致当进入一个比较深的目录 ...

  6. StyleCop学习笔记——初识StyleCop

    一.定义 StyleCop是微软的一个开源的静态代码分析工具,检查c#代码一致性和编码风格. 二.支持的环境. JetBrains R# 5.1.3 ( 5.1.3000.12) JetBrains ...

  7. JavaScript学习笔记-自定义集合类

    //集合类Set( ES6标准才有的类,目前兼容性较差)//自定义集合类:extend = function (o,p){ //定义一个复制对象属性的类函数 for(var x in p){ o[x] ...

  8. JavaScript学习笔记- 自定义滚动条插件

    此滚动条仅支持竖向(Y轴) 一.Css /*这里是让用户鼠标在里面不能选中文字,避免拖动的时候出错*/ body { -moz-user-select: none; /*火狐*/ -webkit-us ...

  9. JavaScript学习笔记-自定义滚动条

    这是一个基本实现思路,如果有新手和我一样没什么事,喜欢瞎研究话,可以参考下. 一.Html <div class="scroll_con"> <div class ...

随机推荐

  1. 反向生成hbm.xml

    选择数据库透视图 打开数据连接 打开刚刚创建的连接,然后打开用户名下的表 这个就是she用户下的表,我们选中要反向生成的表,可以多选,然后点击右键 点击Hibernate Reverse Engine ...

  2. 慕课网-安卓工程师初养成-4-4 Java条件语句之嵌套 if

    来源:http://www.imooc.com/code/1356 嵌套 if 语句,只有当外层 if 的条件成立时,才会判断内层 if 的条件.例如,活动计划的安排,如果今天是工作日,则去上班,如果 ...

  3. DB2 Magazine 中文版: 访问 iSeries 数据

    当您第一次开始学习 DB2 for iSeries 时,一下子要弄清楚如何访问所有数据可能有些令人生畏.我将介绍访问 DB2 for iSeries 数据的一些常见的方法,并展示如何开始开发访问存储在 ...

  4. 【EF 5】结合项目实战分析EF三大工作模式之—Database First

    导读:所谓的EF的Databasefirst工作模式,是目前我们(不涉及社会领域)用的最广的一种模式,也是本次ITOO开发所采用的工作模式.本篇博客,就分析在项目中通过Database First模式 ...

  5. Freebsd下压缩解压文件详解

    压缩篇: 把/usr/webgames目录下的文件打包.命名为bak.tar.gz 放到/usr/db-bak目录里 下面命令可以在任意目录执行.无视当前目录和将要存放文件的目录.tar -zcvf ...

  6. CI系统

  7. 磁盘测试工具fio

    https://wiki.mikejung.biz/Benchmarking #blocksize This options determines the block size for the I/O ...

  8. _config.json

    { "AUTH": "66D86F40DF42A6103C2B0C2F16E41472DABF0594C79859E5EF51E06B377215F3B464E3F0F3 ...

  9. C# winform编程中多线程操作控件方法

    private void Form1_Load(object sender, EventArgs e) { Thread newthread = new Thread(new ThreadStart( ...

  10. angular.foreach 循环方法使用指南

    angular有自己的生命周期.循环给一个 angular监听的变量复值时.最好还是用angular自带的循环方法.“angular.foreach” },{a:}]; angular.forEach ...