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

查看:142
本文介绍了在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 并向下滚动以找到 Code Analysis Rule设置,为其提供文件名,然后搜索并选择以下规则)。可以参见以下规则集文件的内容,您可以复制这些规则集文件并将其粘贴到扩展名为.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.

给出一个规则集文件,可以在 Solution Explorer 面板中右键单击一个项目文件,然后选择 Properties 。在项目属性窗口中,单击左侧面板中的 Code Analysis 标签,然后单击 Open 浏览到.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->在解决方案上运行代码分析 -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)中的无效代码

  • Private methods that are not called from any other code (CA1811)
  • Unused local variables (CA1804)
  • Unused private fields (CA1823)
  • Unused parameters (CA1801)
  • Internal classes that are not instantiated from any other code (CA1812).
  • Dead code in bitwise-OR limited switch (C6259)

下面是.ruleset文件的内容,可以按照上面的步骤操作,以方便您。您可以简单地复制以下XML,将其粘贴到notepad ++中,使用扩展名 .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天全站免登陆