StyleCop学习笔记——自定义规则
本文将简单的一步一步的指导这可能有助于学习如何创建自己的规则
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学习笔记——自定义规则的更多相关文章
- iOS学习笔记-自定义过渡动画
代码地址如下:http://www.demodashi.com/demo/11678.html 这篇笔记翻译自raywenderlick网站的过渡动画的一篇文章,原文用的swift,由于考虑到swif ...
- StyleCop学习笔记——默认的规则
在StyleCop中有一些官方自己写好的检测规则下面就是英文的解释 文档规则 1.SA1600:ElementsMustBeDocumented元素必须添加注释 2.SA1601: PartialEl ...
- StyleCop学习笔记-文档规则
文档规则: .SA1600:ElementsMustBeDocumented元素必须添加注释 .SA1601: PartialElementsMustBeDocumented Partial修饰的成员 ...
- Angular JS 学习笔记(自定义服务:factory,Promise 模式异步请求查询:$http,过滤器用法filter,指令:directive)
刚学没多久,作了一个小项目APP,微信企业号开发与微信服务号的开发,使用的是AngularJS开发,目前项目1.0版本已经完结,但是项目纯粹为了赶工,并没有发挥AngularJS的最大作用,这几天项目 ...
- #Linux学习笔记# 自定义shell终端提示符
我使用的Linux发行版是LinuxMint 17.2 Rafaela,默认情况下Terminal中的shell提示包括了用户名.主机名.当前目录(绝对路径)和提示符.这样会导致当进入一个比较深的目录 ...
- StyleCop学习笔记——初识StyleCop
一.定义 StyleCop是微软的一个开源的静态代码分析工具,检查c#代码一致性和编码风格. 二.支持的环境. JetBrains R# 5.1.3 ( 5.1.3000.12) JetBrains ...
- JavaScript学习笔记-自定义集合类
//集合类Set( ES6标准才有的类,目前兼容性较差)//自定义集合类:extend = function (o,p){ //定义一个复制对象属性的类函数 for(var x in p){ o[x] ...
- JavaScript学习笔记- 自定义滚动条插件
此滚动条仅支持竖向(Y轴) 一.Css /*这里是让用户鼠标在里面不能选中文字,避免拖动的时候出错*/ body { -moz-user-select: none; /*火狐*/ -webkit-us ...
- JavaScript学习笔记-自定义滚动条
这是一个基本实现思路,如果有新手和我一样没什么事,喜欢瞎研究话,可以参考下. 一.Html <div class="scroll_con"> <div class ...
随机推荐
- 关于 profile文件(转)
登录shell执行了两个特殊文件, 1个是:\etc\profile, 这个文件由系统管理员设置,通常做一些如检查是否有邮件,设置默认的创建文件的掩码,给某些表转到处变量赋值,已经任何管理员希望每当用 ...
- EF CRUD 操作
1.Add 操作 public bool Add(EFDataModels.User model) { try { int result=0; using (DBEntities db = new D ...
- org.apache.ibatis.reflection.ReflectionException
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.Reflecti ...
- Android创建自定义dialog方法详解-样式去掉阴影效果
在自定义组件时,从已有组件源码中会很大收获.就拿progressDialog来说 间接父类是dialog,想了解dialog继承结构可以去百度,或者 从构造器来说ProgressDial ...
- 操作笔记:linux下安装ftp
1,安装ftp [root@iZ945sgm0ugZ ~]# yum install vsftpd 安装成功的信息: [root@iZ945sgm0ugZ ~]# yum install vsftpd ...
- ASP.NET MVC 开源项目学习之ProDinner (一)
首先在github上面将ProDinner项目通过 Git Bash 克隆到本地,接下来我们开始分析这个项目吧~ 系统采用.Net 4.5, Asp.net Mvc 5,VS2012,Sql serv ...
- 二模07day2解题报告
T1.采药(medic) 有n个草药,要在m的时间内获得最大价值. 乍一看像是01背包,然而数据只能过50分. 考虑数据范围,t<=10,w<=10,所以只有121种草药.考虑多重背包的二 ...
- 二模10day1解题报告
T1.阅览室(reading) 有一个0~T时间内开放的阅览室,n个读者来读书每人k本,编号和看完所需时间在输入中.其中喜欢度降序排列(不考虑数值),每个人先看喜欢的,如果没有(被人拿走了)就继续找第 ...
- Compass是什么
Compass是一个强大的Sass框架,他的设计目标是顺畅.搞笑地装扮互联网,使用它的人可以写出可维护性更高的样式表. Compass由三个主要部分组成:混混合器和实用工具类库,能够集成到应用开发环境 ...
- cordova 日曆 聯系人 插件使用
日曆插件 聯係人插件 我用聯係人插件, function onSuccess(contact) { alert("Save Success"); }; function onErr ...