.NET静态代码检查工具StyleCop的安装与使用

本文介绍如何使用VisualStudio 2012结合Microsoft托管建议规则及StyleCop工具进行C#代码静态分析,涵盖规则配置、StyleCop安装步骤及具体检查规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近需要做一个C#项目的代码走查,其中的一项内容就是要用代码的静态分析工具对代码进行检测,我的想法是使用VisualStudio自带的Microsoft托管建议规则外加StyleCop工具检测问题(主要是警告),再对这些问题逐一分析。

我使用的VisualStudio版本为2012,Windows版本为Win7旗舰版。一般来说,编译C#代码时VS都会按照Microsoft托管建议规则给出编译报错和警告信息,并支持用户自己配置个性化的ruleset。对于每一个配置项,都可以在MSDN上找到详细的说明文档,地址如下:

https://msdn.microsoft.com/zh-cn/library/ee1hzekz.aspx

(这些规则专注于代码中最关键的问题,包括潜在的安全漏洞、应用程序崩溃和其他重要的逻辑和设计错误。您应在为项目创建的任何自定义规则集中包含此规则集,规则的ruleset文件地址在VS安装目录下的\Team Tools\Static Analysis Tools\Rule Sets\MinimumRecommendedRules.ruleset)

StyleCop支持了另外一个思路的规则,即代码的书写风格。

StyleCop下载地址:http://stylecop.codeplex.com/

我下载的StyleCop版本为4.7.54.0,安装包名为StyleCop-4.7.54.0.msi

第一步,欢迎界面

第二步,授权协议(Microsoft Public License,详见附文)

第三步,自定义安装,其中Visual Studio integration支持VS2008、2010、2012、2013

第四步,确定安装

第五步,读条界面(时间还是比较长的)

第六步,安装完毕

安装完毕后,打开VisualStudio,在程序集上单击鼠标右键,就可以看到多出的三个选项了。

Run StyleCop、Run StyleCop(Rescan All)可用于运行StyleCop做代码检查

StyleCop Settings可设定StyleCop的代码检查规则,设定界面如下:

检查规则设置完毕后,可在程序集所在目录下看到Settings.StyleCop文件,该文件保存了这些检查规则,并可用于下次检查,或复制到其他程序集下,让其他程序集的代码检查也应用这一规则。每次检查后的结果存储在文件StyleCop.Cache里。

StyleCop对C#语言的检查项大致分为以下几类:

-Documentation Rules 文档化注释规则
  -Element Documentaion 变量的文档化注释
  -File Headers 文件头部
-Layout Rules 布局规则
  -Curly Brackets 花括号位置
  -Line Spacing 行距
-Maintainability Rules 稳定性规则
  -Access Modifier 访问修饰符
  -Dubug Test 调试信息
  -File Contents 文件内容
  -Parenthesis 圆括号
  -Removable Code 可移除的代码
  -Naming Rules 标识符命名规则
  -Ordering Rules 排序规则
  -Element Order 变量顺序
  -Using Directives 命名空间引用顺序(Using指令)
-Readability Rules 可读性规则
  -Comments 注释
  -Member Access 成员访问
  -Method Parameter Placement 变量位置
  -Query Expressions 查询表达式
  -Regions Regions区域
  -Statements 声明
  -Strings 字符串
  -Types 类型
-Spacing Rules 空格规则

附:Microsoft Public License全文

Microsoft Public License (Ms-PL)
This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software.
1. Definitions
The terms “reproduce,” “reproduction,” “derivative works,” and “distribution” have the same meaning here as under U.S. copyright law.
A “contribution” is the original software or any additions or changes to the software.
A “contributor” is any person that distributes its contribution under this license.
“Licensed patents” are a contributor’s patent claims that read directly on its contribution.
2. Grant of Rights
(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.
3. Conditions and Limitations
(A) No Trademark License- This license does not grant you rights to use any contributors’ name, logo, or trademarks.
(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.
(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.
(D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.
(E) The software is licensed “as-is.” You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.

转载于:https://my.oschina.net/Tsybius2014/blog/741247

1、无需设置 由于它是一个便携式工具,它不需要您将其安装在目标计算机上,因为只需拆包其存档并启动可执行文件即可完全访问其功能。 您还可以从可移动存储介质(如USB闪存驱动器或外部HDD)运行它。此外,它不会修改系统中的注册表项,也不会在您的PC上生成其他文件或文件夹,但未经您的明确许可。 2、综合界面 dnSpy配备了一个时尚,全面的用户界面,包含广泛的有用功能,它们整齐地组织在主窗口的标准菜单中。 更重要的是,它具有标准配置窗口,您可以在其中调整各种组件的设置,如其反编译器,调试器或十六进制编辑器,但也可以修改显示或其他参数。 3、.NET反汇编程序 如果您需要一个高效的工具,可以通过为您提供各种有用的工具来帮助您反编译.NET程序集,您可以转到dnSpy。它配有一个反编译器,一个调试器,一个十六进制编辑器和一个程序集编辑器,让你以有效的方式处理程序集。 主窗口附带了一个Assembly Explorer窗格,您可以在其中访问所需程序集的树视图。您可以通过从计算机指定适当的文件或使用“从GAC打开”选项打开程序集,该选项为您提供全局程序集缓存中可用的条目列表。 4、.NET程序集的可靠反向工程工具 所有考虑的事情,dnSpy是一个方便的应用程序,使您能够以有效的方式逆向工程.NET程序集,为您提供广泛的有用工具,包括反编译器,程序集编辑器和调试器。它不需要安装,具有全面,平滑的用户界面,并为您提供了一个标准配置窗口,您可以在其中调整各种组件的设置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值