在 Visual Studio 中获取零引用代码列表 [英] Get List of Zero Reference Codes in Visual Studio

查看:49
本文介绍了在 Visual Studio 中获取零引用代码列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Visual Studio 2013 中,特殊代码(方法、属性、字段、...)的引用数量由 代码镜头 显示.我想在 Visual Studio 中获得未使用的(零引用) 代码.有什么办法可以得到吗?

In visual studio 2013 the number of references of a special Code(method, property, field,...) is shown by Code Lens. I want to get unused (zero reference) Codes in visual studio. Is there any way to get them?

推荐答案

可能实现你所追求的最好和最简单的方法是使用 Visual Studio 的内置代码分析工具来查找并直接带你去死代码和未使用的成员.

Probably the best and easiest way to achieve what you are after is to use the build-in code analysis tool with Visual Studio to find and take you directly to dead code and unused members.

为此,我创建了一个新的代码分析规则集文件(通过 File->New->File,确保选择左侧窗格中的 General 并滚动向下找到代码分析规则集,给它一个文件名,然后搜索并选择以下规则).请参阅下面的规则集文件的内容,您可以将其复制并粘贴到要使用的扩展名为 .ruleset 的新文件中.

To this effect, I created a new code analysis ruleset file (Via File->New->File, making sure General in the left pane was selected and scrolling down to find Code Analysis Rule Set, giving it a filename, then searching for and selecting the below rules). See below for the contents of the ruleset file that you can copy, and paste into a new file with the extension .ruleset to use.

给定一个规则集文件,您可以在解决方案资源管理器面板中右键单击一个项目文件,然后选择属性.在项目属性窗口中,单击左侧面板中的代码分析选项卡,然后单击打开以浏览到 .ruleset 文件的位置.如果您转到解决方案文件(而不是项目文件)的属性,您可以在一个位置(在代码分析设置下,并使用下拉菜单以选择规则集文件.注意:但是,您之前必须浏览到规则集文件才能显示在此属性窗口的下拉列表中).

Given a ruleset file, one can right click on a project file in the Solution Explorer panel, and select Properties. In the project properties windows, click on the Code Analysis tab in the left panel, and then click Open to browse to the .ruleset file's location. If you go to the properties of a solution file (as opposed to a project file), you can set the code analysis file for each project in the solution in one place (under Code Analysis Settings, and using the drop-down there to select the ruleset file. NOTE: You must have previously have browsed to the ruleset file for it to show up in the drop-down in this properties window, however).

然后您只需对项目/解决方案运行代码分析(通过 Analyze->Run Code Analysis On Solution -OR- Alt+F11),它就会出现作为警告返回,它找到的任何未引用的方法或未使用的成员.它甚至会找到被某个方法引用的方法,而该方法本身在别处没有引用.

Then you simply run the code analysis on the projects/solution (Via Analyze->Run Code Analysis On Solution -OR- Alt+F11) and it will come back as warnings, any unreferenced methods or unused members it finds. It will even find methods that are referenced by a method, whom itself has no references elsewhere.

但是要小心,因为死代码的代码分析可能会误导您的一种方式是,如果引用被隐藏",只通过委托调用该方法,当然还有反射.

检测死代码的规则,具体来说是:

The rules to detect dead code, specifically, are:

  • 未被任何其他代码调用的私有方法 (CA1811)
  • 未使用的局部变量 (CA1804)
  • 未使用的私有字段 (CA1823)
  • 未使用的参数 (CA1801)
  • 未从任何其他代码 (CA1812) 实例化的内部类.
  • 按位或有限开关 (C6259) 中的死代码

以下是按照上述步骤可以得到的 .ruleset 文件的内容,为您提供方便.您可以简单地复制以下 XML,将其粘贴到记事本++中,使用扩展名 .ruleset 保存在某处,按照上述说明浏览并使用:

Below is the contents of the .ruleset file that can be had by following the steps above, for your conveinence. You can simply copy the below XML, paste it into notepad++, save somewhere with the extension .ruleset, browse for and use as explained above:

<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Dead Code Rules" Description=" " ToolsVersion="12.0">
  <Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
    <Rule Id="CA1801" Action="Warning" />
    <Rule Id="CA1804" Action="Warning" />
    <Rule Id="CA1811" Action="Warning" />
    <Rule Id="CA1812" Action="Warning" />
    <Rule Id="CA1823" Action="Warning" />
  </Rules>
  <Rules AnalyzerId="Microsoft.Analyzers.NativeCodeAnalysis" RuleNamespace="Microsoft.Rules.Native">
    <Rule Id="C6259" Action="Warning" />
  </Rules>
</RuleSet>

这篇关于在 Visual Studio 中获取零引用代码列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆