简体   繁体   English

列出 Visual Studio 2019 中未引用的文件?

[英]List unreferenced files in Visual Studio 2019?

I have been experimenting a lot inside a solution containing only .NET Core 3.1 projects, and I noticed that there was a lot of unnecessary/unused code as a result.我在仅包含.NET Core 3.1项目的解决方案中进行了大量试验,结果我注意到有很多不必要/未使用的代码。

Is there a way to list all the code inside my solution that isn't referenced anywhere else?有没有办法列出我的解决方案中没有在其他任何地方引用的所有代码?

I'm aware that Visual Studio 2019 has the CodeLens feature that shows if a piece of code is referenced, but checking it for every single file manually is obviously not what I'm going for.我知道 Visual Studio 2019 具有 CodeLens 功能,可以显示是否引用了一段代码,但手动检查每个文件显然不是我想要的。

In existing answers for older versions of Visual Studio, the FxCop tool is mentioned a lot but as you can see on the Microsoft Page, it's no longer being updated.在旧版本 Visual Studio 的现有答案中, 经常提到FxCop 工具,但正如您在 Microsoft 页面上看到的那样,它不再更新。 I also tried to run Code Analysis on my solution, but since I couldn't configure the analysis parameters for my solution, that didn't lead anywhere either.我还尝试在我的解决方案上运行代码分析,但由于我无法为我的解决方案配置分析参数,这也没有任何意义。 I even tried to configure code analysis on the project level, but Visual Studio didn't recognize my project as a candidate for analysis.我什至尝试在项目级别配置代码分析,但 Visual Studio 没有将我的项目识别为分析的候选对象。

You can use the RuleSet from this answer to make your CodeAnalysis show you unused code:您可以使用此答案中的规则集让您的 CodeAnalysis 向您显示未使用的代码:

<?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>

Edit: You can use this step by step guide to build your RuleSet.编辑:您可以使用此分步指南来构建您的规则集。

Edit 2: Since the step by step guide is outdated, here is a new way of activating the needed rules:编辑 2:由于分步指南已经过时,这里有一种激活所需规则的新方法:

  1. Go to the properties page of your project (right click -> Properties)转到项目的属性页面(右键单击 -> 属性)
  2. Go to "Code Analysis", the last tab转到“代码分析”,最后一个选项卡
  3. Click "Configure"点击“配置”
  4. Enter "unused" and activate all the rules that you want输入“未使用”并激活您想要的所有规则
  5. On the right, optionally set the Action to "Error" for extra attention在右侧,可选择将操作设置为“错误”以获得额外注意
  6. Save (ctrl+s)保存(ctrl+s)

Edit 3: We will keep the ruleset but it turns out I had an old project and on new ones, you need to do the following:编辑 3:我们将保留规则集,但事实证明我有一个旧项目和新项目,您需要执行以下操作:

  1. If your target framework is below .NET5, install Microsoft.CodeAnalysis.FxCopAnalyzers Microsoft.CodeAnalysis.NetAnalyzers from NuGet ( FxCop was deprecated )如果您的目标框架低于 .NET5,请从 NuGet 安装Microsoft.CodeAnalysis.FxCopAnalyzers Microsoft.CodeAnalysis.NetAnalyzers不推荐使用 FxCop

  2. If you haven't already, add a file .editorconfig to you solution如果您还没有,请将文件.editorconfig添加到您的解决方案中

  3. Enable the rules you want like this:像这样启用你想要的规则:

    [*.cs]

    dotnet_diagnostic.CA1822.severity = error

in the editorconfig file to activate them.在 editorconfig 文件中激活它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Visual Studio 中获取零引用/未引用代码列表 - How to get list of zero-reference/unreferenced code in Visual Studio 为什么 Visual Studio 2019 会删除对新 .razor 文件的监视? - Why does Visual Studio 2019 remove watch for new .razor files? 在调试Visual Studio 2019,WPF时编辑Xaml文件 - Edit Xaml files on debuging Visual studio 2019, WPF Visual Studio 2019 无法识别从 GitHub 提取的新文件 - Visual Studio 2019 not Recognizing new Files Pulled from GitHub 如何在 Visual Studio 2019 模板列表中安装 DevExtreme 应用程序如下 - How to install DevExtreme Application in Visual studio 2019 templates list as below Visual Studio 2019 中的构建错误在错误列表中显示不一致 - Build Errors in Visual Studio 2019 inconsistently show up in Error List 工具提示 Visual Studio 2019 的问题 - Problems with Tooltip Visual Studio 2019 无法在 Visual Studio 2019 Preview 和 Visual Studio 2019 中添加引用 - Unable to add references in Visual studio 2019 Preview and Visual Studio 2019 在 Visual Studio 2019 中升级 xproj - Upgrading xproj in Visual Studio 2019 Visual Studio 2019检查NullableContextOptions - Visual Studio 2019 inspections for NullableContextOptions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM