整个解决方案的全局抑制(C#)

问题描述 投票:0回答:3

你们中有人知道我可以抑制的方法吗? CA2000 用于整个解决方案?

我正在考虑类似

GlobalSuppressions
类的东西,其中包含以下代码行:

[assembly: SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope")]

感谢您提前的帮助。

c# code-analysis suppression
3个回答
8
投票

可以在 .editorconfig 文件中配置全局抑制。

  1. 在解决方案的根文件夹中创建一个名为 .editorconfig 的文件
  2. 在文本编辑器中打开
  3. 在第一行添加
    root = true
  4. 在该行下方添加要禁用的规则
    [*.{cs,vb}]
    dotnet_diagnostic.<Rule_Code>.severity=silent
    

这是一个示例 .editorconfig 文件

root = true

[*.{cs,vb}]
dotnet_diagnostic.CA2000.severity=silent

[*.{cs,vb}]
dotnet_diagnostic.MA0004.severity=silent    # Even works with 3rd part rules/analyser 

5
投票

如果有人偶然发现这一点,可以创建一个全局抑制文件(例如:

GlobalSuppressions.cs
),将其“作为链接”添加到您需要的每个项目中。它应该包含如下行:

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task", Justification = "ASP.NET Core doesn't use thread context to store request context.", Scope = "module")]

有关更多详细信息,请参阅:https://learn.microsoft.com/en-us/visualstudio/code-quality/in-source-suppression-overview?view=vs-2022#suppress-violations-using-a-global -抑制文件


2
投票

执行此操作的最佳方法是创建一个

Directory.Build.props
文件,该文件将自动包含在其所在子文件夹中的所有项目中。然后将以下内容添加到文件中:

<?xml version="1.0" encoding="utf-8" ?>
<Project>
  <!-- This file will be automatically included in all projects because of its name -->
  <PropertyGroup>
    <NoWarn>CA2000</NoWarn>
  </PropertyGroup>
</Project>
© www.soinside.com 2019 - 2024. All rights reserved.