孙展波:实现.NET无限潜力

Realize .NET Potential (中文版)
随笔 - 417, 评论 - 12186, 引用 - 54

导航

关于

贴子以"现状"提供且没有任何担保也没有授予任何权利。
计数器(2005/12/8起).点击阅读我的Blog In English

标签

每月存档

最新留言

广告

 

2年前的Tech Ed China上就有过FxCop的介绍,当时译为“框架警察”。关于FxCop的介绍和若干规则示例,可以参考John Robbins所写的Bad Code? FxCop to the RescueThree Vital FXCop Rules文章。本系列将由浅入深的解释定制FxCop的常用方式和技巧。

 

设计新类时要避免实现ICloneableBlog中仅仅解释了其原因。对于Design Guideline的实施,可以经常得到FxCop的帮助。既然FxCop自带的规则还没有包括对这一设计指导的检查,让我们自己来写一个这样的规则。全部步骤如下。使用的是.NET框架1.1VS2003FxCop 1.312版本,注意FxCop规则编程现在并没有得到官方支持。如果以后版本API发生变化,我会对示例程序有选择的更新。

  1. 下载FxCop 1.312: http://www.gotdotnet.com/team/fxcop/ 选择FxCop for .NET 1.1.
  2. 安装FxCop 1.312, 其缺省安装在“%ProgramFiles%\Microsoft FxCop 1.312”文件夹下。注意其中包括的FxCopSdk.dllMicrosoft.Cci.DLL2个程序集(assembly)。我们的规则项目需要引用它们。
  3. 启动FxCop, 留意到在Rules Tab下所有预先提供的Rules.
  4. 设计新类时要避免实现ICloneable中提供的测试用例编译成为一个Class library起名为FxCopRuleTests
  5. 通过菜单,工具栏或者Ctrl+Shift+A以增加Target,即这些Rule将检查的程序集。添加FxCopRuleTests
  6. 通过菜单,工具栏或者F5开始分析。结果有7violation messages,但是没有设计新类时要避免实现ICloneable相关的消息。我们现在就开始写这样的一个FxCop Rule.
  7. 使用VS2003创建新的Class Library项目起名CustomFxCopRules.
  8. 删除IDE创建的Class1.csAssemblyInfo.cs.
  9. 添加对FxCopSdk.dllMicrosoft.Cci.DLL的引用
  10. 增加一个类文件叫做AvoidICloneableImplementation.cs.
  11. 添加一个命名为RuleInfo.xmlXML文件,设置其Build ActionEmbedded Resource.
  12. 使用如下的代码,实现这一简单的FxCop Rule,编译后通过菜单,工具栏或者Ctrl+R加入FxCopRule之中,再运行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

留言反馈

#tgyznqmg - Google Search 编辑
tgyznqmg - Google Search
2008-10-26 22:21:41 | [匿名用户:]
#mgouakfs - Google Search 编辑
mgouakfs - Google Search
2008-09-25 15:55:09 | [匿名用户:]
#回复: 定制FxCop规则示例之一:AvoidICloneableImplementation 编辑
同樣是:
Error loading rule 'AvoidICloneableImplementation': The following stream was not found in the assembly manifest: ZhanboBlog.Demo.CustomRules.RuleInfo.xml

在VS2005下編譯的。請問是什么原因?
2008-01-17 17:29:00 | [匿名用户:球球]
#回复: 定制FxCop规则示例之一:AvoidICloneableImplementation 编辑
2007-11-20 11:20:00 | [匿名用户:runescape money]
#free music video codes for myspace pages 编辑
free music video codes for myspace pages
2007-09-18 00:22:00 | [匿名用户:free music video codes for myspa]
#Thanks 编辑
Thank you! Great work, i have added it in bookmarks.
2007-04-03 17:56:00 | [匿名用户:Jessica]
#Thanks 编辑
Thank you! Great work, i have added it in bookmarks.
2007-04-03 17:55:00 | [匿名用户:Jessica]
#re: 定制FxCop规则示例之一:AvoidICloneableImplementation 编辑
<Resolution>Type '{0}' Implements ICloneable, which should be avoided.</Resolution>改为<Resolution>Type {0} Implements ICloneable, which should be avoided.</Resolution>。
2006-04-17 14:25:00 | [匿名用户:ws]
#re: 定制FxCop规则示例之一:AvoidICloneableImplementation 编辑
按照上面的操作步骤之后,发现以下问题
Error loading rule 'AvoidICloneableImplementation': The following stream was not found in the assembly manifest: ZhanboBlog.Demo.CustomRules.RuleInfo.xml

请问是什么原因。
2006-03-21 17:39:00 | [匿名用户:HowToDo]
#re: 定制FxCop规则示例之一:AvoidICloneableImplementation 编辑
有这样一个场景,公司定义了很多类型的命名规范,比如DataSet dstProduct = new DataSet(),其中Product前加了三位代码dst.
FxCop是否可以对命名规范进行检查呢?怎么扩展这个规则?
2005-10-01 17:39:00 | [匿名用户:cowbird]
#sealed能提高性能 编辑
如果根据设计一个类可以被sealed,则我们应该这样做。
2005-07-07 15:19:00 | [匿名用户:longer]
#Fxcop 编辑
TrackBack From:http://www.cnblogs.com/alex.zhang/archive/2005/05/31/165273.html
2005-05-31 09:42:00 | [匿名用户:Alex]
#re: 定制FxCop规则示例之一:AvoidICloneableImplementation 编辑
He is not odd. He is sick.
2005-03-15 11:05:00 | [匿名用户:Ninputer]
#re: 定制FxCop规则示例之一:AvoidICloneableImplementation 编辑
you know, i am an odd person.everything attract me, but mostly just to 'know about'(not too deep) the new stuff.today,i am only take my time on english language skills.whatever,you are luck guy, because of your strong english writing skills.

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,
2005-03-14 19:11:00 | [匿名用户:leighsword]
#re: 定制FxCop规则示例之一:AvoidICloneableImplementation 编辑
Well, leighsword, I spent the whole weekend trying to use Unstacker to work around a limitation of FxCop engine. I bet someone will be interested in FxCop, you included, since you are asking where you can download new rules.

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.
2005-03-14 12:39:00 | [匿名用户:孙展波]
#re: 定制FxCop规则示例之一:AvoidICloneableImplementation 编辑
where can i download the NEW rules for fxcop?
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.
2005-03-14 12:19:00 | [匿名用户:leighsword]
对不起,目前本随笔不允许发表新评论.

Powered by: Joycode.MVC引擎 0.5.1.8