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 ...
随机推荐
- freeglut第一步
#include <GL/freeglut.h> static void RenderSceneCB() { glClear(GL_COLOR_BUFFER_BIT); glutSwapB ...
- Java后台工程师面试杂记——不跳不涨工资星人跳槽经历
经过接近一个月的时间,完成换工作这件“小事”,前后总计面试了多家公司,最后也没接到几个offer,不过最终总算尘埃落定,就对这个过程进行一个总结吧. 在某互联网公司工作了近一年的时间,但是频繁的业务需 ...
- socket学习笔记——IO口的基本操作(读、写)
写操作 1 #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unist ...
- sqool导出oracle数据
set colsep '|' --设置|为列分隔符 set echo off --在用start命令执行一个sql脚本时,是否显示脚本中正在执行的SQL语句 set fee ...
- JavaScript事件小结
我们都晓得JavaScrip事件的重要性,所以下面小结一下以备后用! 序号 事件 描述 备注 onclick 鼠标点击某个对象时触发此事件 是最常用的事件之一 onchange 用户改变域的内容时 ...
- new 、operator new 和 placement new
一.原生operator new 我们先从原生operator new开始.考虑如下代码,它用来分配5个int型的空间并返回指向他们的指针[1]: int* v = static_cast<in ...
- jquery attr()和prop()方法的使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ASP.NET的SEO:Linq to XML---网站地图和RSS Feed
本系列目录网站地图的作用是让搜索引擎尽快的,更多的收录网站的各个网页. 这里我们首先要明白一个基本的原理,搜索引擎的爬行方式.整个互联网就像一张纵横交错的"网":网的各个节点 ...
- linux 编译,链接和加载
1. 序 最近在折腾各种.so,碰到了一些问题,一开始对于很多错误也没有头绪,茫然不知所措.索性化了一天多时间将<<程序员的自我修养—链接.装载与库>>中部分内容略读了一遍 ...
- windows installer 出错问题解决
在卸载程序的额时候,如果出现windows installer出错,可以通过一个Windows Installer CleanUp Utility, 有了Windows Installer Clean ...