在2年前的Tech Ed China上就有过FxCop的介绍,当时译为“框架警察”。关于FxCop的介绍和若干规则示例,可以参考John Robbins所写的Bad Code? FxCop to the Rescue和Three Vital FXCop Rules文章。本系列将由浅入深的解释定制FxCop的常用方式和技巧。
在设计新类时要避免实现ICloneable的Blog中仅仅解释了其原因。对于Design Guideline的实施,可以经常得到FxCop的帮助。既然FxCop自带的规则还没有包括对这一设计指导的检查,让我们自己来写一个这样的规则。全部步骤如下。使用的是.NET框架1.1和VS2003和FxCop 1.312版本,注意FxCop规则编程现在并没有得到官方支持。如果以后版本API发生变化,我会对示例程序有选择的更新。
- 下载FxCop 1.312: http://www.gotdotnet.com/team/fxcop/ 选择FxCop for .NET 1.1.
- 安装FxCop 1.312, 其缺省安装在“%ProgramFiles%\Microsoft FxCop 1.312”文件夹下。注意其中包括的FxCopSdk.dll和Microsoft.Cci.DLL这2个程序集(assembly)。我们的规则项目需要引用它们。
- 启动FxCop, 留意到在Rules Tab下所有预先提供的Rules.
- 将设计新类时要避免实现ICloneable中提供的测试用例编译成为一个Class library起名为FxCopRuleTests。
- 通过菜单,工具栏或者Ctrl+Shift+A以增加Target,即这些Rule将检查的程序集。添加FxCopRuleTests。
- 通过菜单,工具栏或者F5开始分析。结果有7条violation messages,但是没有设计新类时要避免实现ICloneable相关的消息。我们现在就开始写这样的一个FxCop Rule.
- 使用VS2003创建新的Class Library项目起名CustomFxCopRules.
- 删除IDE创建的Class1.cs和AssemblyInfo.cs.
- 添加对FxCopSdk.dll和Microsoft.Cci.DLL的引用
- 增加一个类文件叫做AvoidICloneableImplementation.cs.
- 添加一个命名为RuleInfo.xml的XML文件,设置其Build Action为Embedded Resource.
- 使用如下的代码,实现这一简单的FxCop Rule,编译后通过菜单,工具栏或者Ctrl+R加入FxCop的Rule之中,再运行FxCop看到正确结果。(提示,可以uncheck所有FxCop自带规则以减少干扰。
这是RuleInfo.xml
<?xml version="1.0" encoding="utf-8" ?>
<Rules FriendlyName="Custom FxCop Rules">
<Rule TypeName="AvoidICloneableImplementation" Category="ZhanboBlog.Demo" CheckId="ZB001">
<Name>Avoid ICloneable Implementation</Name>
<Description>ICloneable could be used to return either deep copy or shallow copy, and is therefore not useful.</Description>
<Url>http://blog.joycode.com/zhanbos/archive/2005/03/13/45707.aspx</Url>
<Resolution>Type '{0}' Implements ICloneable, which should be avoided.</Resolution>
<Email />
<MessageLevel Certainty="99">Error</MessageLevel>
<FixCategories>Breaking</FixCategories>
<Owner />
</Rule>
</Rules>
这是AvoidICloneableImplementation.cs
using System;
using Microsoft.Cci;
using Microsoft.Tools.FxCop.Sdk;
using Microsoft.Tools.FxCop.Sdk.Introspection;
namespace ZhanboBlog.Demo.CustomRules
{
/// <summary>
/// Do not implement ICloneable interface
/// </summary>
public class AvoidICloneableImplementation : BaseIntrospectionRule
{
public AvoidICloneableImplementation() :
base("AvoidICloneableImplementation", "ZhanboBlog.Demo.CustomRules.RuleInfo", typeof(AvoidICloneableImplementation).Assembly)
{
}
public override ProblemCollection Check(TypeNode type)
{
if (DoesImplementICloneable(type.Interfaces))
{
Resolution resolution = GetResolution(RuleUtilities.Format(type));
Problem newProblem = new Problem(resolution, type);
Problems.Add(newProblem);
return Problems;
}
return null;
}
private bool DoesImplementICloneable(InterfaceList interfaceList)
{
for(int i = 0; i < interfaceList.Length; i++)
{
if (interfaceList[i] == SystemTypes.ICloneable)
{
return true;
}
}
return false;
}
}
}
如果您对某些代码有疑惑,可以留下评论我会在下一次Blog中提供必须的解释。通过实践会加深认识。您有什么希望增加FxCop规则检查的情形也欢迎提出,作为以后FxCop示例的参考。
贴子以"现状"提供且没有任何担保也没有授予任何权利
打印 | 张贴于 2005-03-13 11:41:00 | Tag:设计指导 | Design Guideline
留言反馈
Error loading rule 'AvoidICloneableImplementation': The following stream was not found in the assembly manifest: ZhanboBlog.Demo.CustomRules.RuleInfo.xml
在VS2005下編譯的。請問是什么原因?
http://www.vgoldsupply.com
http://www.runescapemoney-runescapegold.cn
http://www.vgoldseller.com/runescape-c-599.html
Error loading rule 'AvoidICloneableImplementation': The following stream was not found in the assembly manifest: ZhanboBlog.Demo.CustomRules.RuleInfo.xml
请问是什么原因。
FxCop是否可以对命名规范进行检查呢?怎么扩展这个规则?
i am used to read english TECH articles,not chinese tech articles(it makes me sick), and i hope you can write your articles in english,
The one on that web page is the current version. New version, when posted, will add new rules but rarely remove old ones. Even if there are many violations you initially get, most make sense and for the rest, you can exclude them.
There are different ways to exclude violation warnings, which I will touch in later posts.
We recognize that some assemblies produced from Microsoft also have violations. In actuality, FxCop SDK classes itself have many naming issues, which is one reason they are not officially supported. Future assemblies and tools (such as VS2005) from Microsoft will do much better job here.
because every assembler(include MS assembler) will got lots of warnning infos when to using the default rules of fxcop .
PS: no one will care the fxcop,so you should not post the article in here.