Fxcop 初体验

  代码质量对于软件项目的成败很重要,这点我想大家都明白。那么在一个软件团队中如何保证代码质量呢?对于这个问题不同的人可能会有不同的答案,对于我而言我觉得做好两点代码质量基本就可以保证了:

  1.代码规范(具体规范细则可以参考.NET 设计规范 一书)

  2.测试(对于程序员本身而言主要是单元测试)

  微软提供了 FoxCop 来检查代码规范

  Nunit 来完成单元测试

  对于Nunit 本博客已经有了一篇入门的文章,现在再来介绍 FxCop 的入门(由于个人的喜好,我不喜欢一些工具如SVN、NUnit等和VS集成,本文也不会弹 FxCop 与 VS的集成,但是网上这类文章很多,需要的读者自行搜索)

一、Fxcop 软件的安装

  你可以从各种途径搞到FxCop 的安装包,我这里也提供 FxCop1.36 的下载

  下载后按照提示安装,安装好后启动 FxCop

  

二、检查程序集是否符合代码规范

Project --> Add Targets --> Analyze 如下图所示(在Rules Tab 中我们可以选择启用的规则)

三、自定义规则

1. 新建 类库项目加入 FxCop 1.36 安装目录下的 FxCopSdk.dll 和 Microsoft.Cci.dll

2. 新建 规则 xml,如下所示,并编译为资源

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <Rules FriendlyName="XXXSoft公司命名规范">
 3     <Rule TypeName="XXXSoftNameRuleMethod" Category="XXXSoft.NameRule" CheckId="XX0001">
 4         <Name>方法名规则</Name>
 5         <Description>方法名必须首字母大写,如果首字符小写将提示警告。</Description>
 6         <Url/>
 7         <Resolution>方法 “{0} ”的首字母应为大写</Resolution>
 8         <MessageLevel Certainty="99">Error</MessageLevel>
 9         <Email>yourMail@gmail.com</Email>
10         <FixCategories>NonBreaking, DependsOnFix</FixCategories>
11         <Owner>ServiceSoft</Owner>
12     </Rule>
13 
14     <Rule TypeName="XXXSoftNameRuleField" Category="XXXSoft.NameRule" CheckId="XX0002">
15         <Name>字段名规则</Name>
16         <Description>字段的访问修饰符不能是公有的。</Description>
17         <Url/>
18         <Resolution>字段 “{0} ”的访问修饰符是public</Resolution>
19         <MessageLevel Certainty="99">Error</MessageLevel>
20         <Email>yourMail@gmail.com</Email>
21         <FixCategories>NonBreaking, DependsOnFix</FixCategories>
22         <Owner>ServiceSoft</Owner>
23     </Rule>
24 </Rules>
View Code

3. 写规则类

 1 using Microsoft.FxCop.Sdk;
 2 
 3 namespace XXXCompanyRules
 4 {
 5     public class FieldRule1 : BaseIntrospectionRule
 6     {
 7         public FieldRule1() :
 8             base("XXXSoftNameRuleField", "XXXCompanyRules.Rules", typeof(FieldRule1).Assembly)
 9         {}
10 
11         public override ProblemCollection Check(Member member)
12         {
13             if (member.DeclaringType is EnumNode)
14             {
15                 return null;
16             }
17             Field field = member as Field;
18             if (field == null)
19             {
20                 return null;
21             }
22             if (field.IsPublic)
23             {
24                 Problems.Add(new Problem(GetResolution(member.Name.Name)));
25             }
26             return Problems;
27         }
28     }
29 }
View Code
 1 using System;
 2 using Microsoft.FxCop.Sdk;
 3 
 4 namespace XXXCompanyRules
 5 {
 6     public class MethodRule1 : BaseIntrospectionRule
 7     {
 8         public MethodRule1() :
 9             base("XXXSoftNameRuleMethod", "XXXCompanyRules.Rules", typeof(MethodRule1).Assembly)
10         { }
11 
12         /// <summary>
13         /// 验证首字母
14         /// </summary>
15         /// <param name="member">类型成员</param>
16         /// <returns>错误提示</returns>
17         public override ProblemCollection Check(Member member)
18         {
19             Method method = member as Method;
20             if (method == null)
21             {
22                 return null;
23             }
24             if (!method.IsSpecialName && !Char.IsUpper(method.Name.Name, 0))
25             {
26                 Problems.Add(new Problem(GetResolution(member.Name.Name)));
27             }
28             return Problems;
29         }
30     }
31 }
View Code

4. 编译生成 规则 dll

5. 添加到 FxCop 中

6. 验证

本节代码下载

本文完

原文地址:https://www.cnblogs.com/Aphasia/p/4177402.html