Fxcop使用参考

1Fxcop配置文件简介,2命令行介绍,3项目中使用Fxcop,4Fxcop与VS集成

1Fxcop配置文件简介

   Fxcop配置文件也就是Fxcop工程文件,也即是后缀为FxCop的文件

文件内容和简单,例如:

<?xml version="1.0" encoding="utf-8"?>
<FxCopProject Version="1.36" Name="My FxCop Project">
 <ProjectOptions>
  <SharedProject>True</SharedProject>
  <Stylesheet Apply="False">c:\program files\microsoft fxcop 1.36\Xml\FxCopReport.xsl</Stylesheet>
  <SaveMessages>
   <Project Status="Active, Excluded" NewOnly="False" />
   <Report Status="Active" NewOnly="False" />
  </SaveMessages>
  <ProjectFile Compress="True" DefaultTargetCheck="True" DefaultRuleCheck="True" SaveByRuleGroup="" Deterministic="True" />
  <EnableMultithreadedLoad>True</EnableMultithreadedLoad>
  <EnableMultithreadedAnalysis>True</EnableMultithreadedAnalysis>
  <SourceLookup>True</SourceLookup>
  <AnalysisExceptionsThreshold>10</AnalysisExceptionsThreshold>
  <RuleExceptionsThreshold>1</RuleExceptionsThreshold>
  <Spelling Locale="en-US" />
  <OverrideRuleVisibilities>False</OverrideRuleVisibilities>
  <CustomDictionaries SearchFxCopDir="True" SearchUserProfile="True" SearchProjectDir="True" />
  <SearchGlobalAssemblyCache>False</SearchGlobalAssemblyCache>
  <DeadlockDetectionTimeout>120</DeadlockDetectionTimeout>
  <IgnoreGeneratedCode>False</IgnoreGeneratedCode>
 </ProjectOptions>
 <Targets>
  <AssemblyReferenceDirectories> 
   <Directory>C:/Windows/Microsoft.NET/Framework/v2.0.50727/</Directory>
   <Directory>C:/Windows/Microsoft.NET/Framework/v4.0.30319/WPF/</Directory>
  </AssemblyReferenceDirectories>
  <Target Name="$(ProjectDir)/bin/Debug/DataContract.dll" Analyze="True" AnalyzeAllChildren="True" />
  <Target Name="$(ProjectDir)/bin/Debug/DebugLogger.dll" Analyze="True" AnalyzeAllChildren="True" /> 
 </Targets>
 <Rules>
  <RuleFiles>
   <RuleFile Name="$(FxCopDir)\Rules\DesignRules.dll" Enabled="False" AllRulesEnabled="False">
    <Rule Name="AbstractTypesShouldNotHaveConstructors" Enabled="True" />
   </RuleFile>
   <RuleFile Name="$(FxCopDir)\Rules\GlobalizationRules.dll" Enabled="False" AllRulesEnabled="False" />
   <RuleFile Name="$(FxCopDir)\Rules\InteroperabilityRules.dll" Enabled="False" AllRulesEnabled="False" />
   <RuleFile Name="$(FxCopDir)\Rules\MobilityRules.dll" Enabled="False" AllRulesEnabled="False" />  
  </RuleFiles>
  <Groups />
  <Settings />
 </Rules>
</FxCopProject>
  <AssemblyReferenceDirectories>一节包含查找引用程序集的目录,使用Fxcop时,经常遇到引用的程序集找不到的情况,可以使用Fxcop界面添加程序集,或者手动在这一节中添加目录,尤其是在个人电脑共有程序集路径不同的情况下。

<Target>包含需要检测的程序集,

RuleFile 包含检测规则文件所在的路径,以及是否检测。

<SearchGlobalAssemblyCache>False</SearchGlobalAssemblyCache> 

如果为True,则当需要检测的程序集引用的程序集找不到时,会到系统全局程序集中找,否则不会在系统全局程序集中找,直接会报找不到程序集的错误,所以建议设置为True,默认是False。

需要注意的是Fxcop配置文件好像只能识别$(ProjectDir),其他和项目相关的路径好像都不识别,例如TargetDir。尤其是在开发机和服务器TargetDir目录相对于项目不一致时,很容易出现路径不一致的问题。

现在理解了$(ProjectDir)的含义,它是指Fxcop工程文件,也即是后缀为FxCop的文件的目录,而不是源代码解决方案的目录,自然没有TargetDir之类的东西了
2命令行介绍

官网参考

3项目中使用Fxcop

可以在项目的属性中的build event中调用Fxcop进行检测例如:FxCopCmd.exe  /c /p:"$(SolutionDir)SampleSolution.FxCop"

检测出来不符合要求的默认是警告,不是错误。如果要把警告改成错误,有几种思路。

思路一这种很复杂,我自己也没有看懂。

思路二这种看起来简单多了,但自己没试,不知道能不能用。

思路三就得自己写个小程序,把Fxcop的警告输出到特定文件,然后分析这个文件。

4Fxcop与VS集成

Fxcop使用笔记

Pre-build Event/Post-build Event参考

Visual Studio build event command line 使用方法

Adding FxCopCmd to Your Build Process

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值