[英]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项目的解决方案中进行了大量试验,结果我注意到有很多不必要/未使用的代码。
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:由于分步指南已经过时,这里有一种激活所需规则的新方法:
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:我们将保留规则集,但事实证明我有一个旧项目和新项目,您需要执行以下操作:
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 )
If you haven't already, add a file .editorconfig
to you solution如果您还没有,请将文件.editorconfig
添加到您的解决方案中
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.