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

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. 翻译:wiki中的business logic词条

    Business logic 业务逻辑 From Wikipedia, the free encyclopedia 来自Wikipedia,自由的百科全书 In computer software, ...

  2. 洛谷P1518 两只塔姆沃斯牛 The Tamworth Two

    P1518 两只塔姆沃斯牛 The Tamworth Two 109通过 184提交 题目提供者该用户不存在 标签USACO 难度普及+/提高 提交  讨论  题解 最新讨论 求数据 题目背景 题目描 ...

  3. 图形化Cisco设备管理实践(附安装配置视频)

    图形化Cisco设备管理实践 Ciscoworks 2000是Cisco公司推出的基于SNMP协议的网络管理系统,通过它网络管理人员可以方便快捷地完成设备的配置.管理.监控和故障分析等任务, Cisc ...

  4. 如何用手机维护Mysql数据库

    如何用手机维护网站数据库 身边很多人都在拿ipad或iphone来玩儿游戏或听歌,大多数人认为它们就是个娱乐设备,在我看来它们同样可以帮助我们更加快捷的工作,我用手机遥控单反.用手机控制PPT的播放. ...

  5. jqmobi 转换语言

    当第一次打开APP时,检测手机默认的语言,设置APP的语言跟手机默认一样:当点击了APP里面的设置语言的按钮,存储当前设置的语言 :关闭APP:再一次打开APP时,检测存储在APP里面的语言,转换语言 ...

  6. Windbg CLR基础小测 《第六篇》

    首先写一段代码如下: namespace ConsoleApplication3 { class Program { static void Main(string[] args) { Console ...

  7. .net HttpClient类(System.Net.Http) c#并发使用测试、压测

    .net HttpClient类(System.Net.Http) c#并发使用测试.压测

  8. 【Linux】freetds安装配置连接MSSQL

    我使用的是freetds-0.91,下载地址:http://pan.baidu.com/s/1hq68rZY 安装编译(根据需要unixodbc): [root@zabbixserver / ]# t ...

  9. libpcap报文解析: ipv4、ipv6(待优化)

    #include <string.h> #include <stdlib.h> #include <pcap.h> #include <netinet/in. ...

  10. Hbase rest方式获取指定key范围内的值

    代码如下: <?php class Monitor_Hbase{ private $rest_host = "http://10.99.90.39:8130/";//rest ...