禁止Visual Studio中的所有项目的警告

isp*_*iro 13 .net c# suppress-warnings visual-studio

我已经看到答案显示如何抑制特定代码行或特定项目的警告.我不希望这样.

我想要禁止所有项目的特定警告.

(如果重要,警告是IDE0044.我正在使用C#.)

Joh*_*ner 9

Visual Studio 2017(15.7.1)的最新更新现在有一个选项.在Tools->Options菜单下,选择TextEditor->C#->Code Style->General选项卡.在Field preferences,有一个Prefer readonly选项.设置为No.

文本编辑器首选项的图像.

editorconfig如果要在代码旁边检查此首选项,还可以设置一个设置,因此使用代码的其他人不会收到警告,但必须在每个解决方案的基础上完成.editorconfig您将设置的值为:

 dotnet_style_readonly_field = false:none
Run Code Online (Sandbox Code Playgroud)


Rah*_*hul 5

您可以使用命名空间SuppressMessage下存在的属性,System.Diagnostics.CodeAnalysis

[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "args")]
Run Code Online (Sandbox Code Playgroud)

好吧,正如您所编辑的那样,我想为我的所有项目取消特定警告

对于整个项目明智的 AFAIK,您不能这样做。但是如果有帮助,请检查链接的帖子一次

如何抑制所有类型成员的代码分析消息?

  • 谢谢。但这仅适用于一个项目。如果我错了,请纠正我。(不过,我不是反对者。) (3认同)

Vla*_*lav 5

您可以尝试使用Directory.Build.props添加NoWarn属性来发出特定警告。不过我还没有验证过。

正如另一个答案中所说,最好解决根本原因,而不是忽略它。


Ste*_*edy 5

To suppress warnings for all projects, you need to create a .editorconfig file in a top-level directory. For example, I have mine in trunk and I commit it to source control so that my colleagues share the same settings.

The settings in this file apply to all projects in trunk and subfolders, unless overridden by another .editorconfig file further down the folder tree e.g. you might you have a project specific EditorConfig file in a subfolder which has different settings. See File hierarchy and precedence for more details.

Creating an EditorConfig file

You can use a text editor for this if you just want to change one specific setting. However, Visual Studio can create a .editorconfig file with sensible defaults for .NET for you. From MSDN:

  • Create a new project

  • From the menu bar, choose Project > Add New Item; or press Ctrl+Shift+A

  • Select the editorconfig File (.NET) template to add an EditorConfig file prepopulated with default .NET code style, formatting, and naming conventions

在此输入图像描述

  • Optionally delete the project - we don't really need it

Visual Studio 2019 - Creating an EditorConfig file from current settings

In Visual Studio 2019, you can instead create an EditorConfig file from your current settings. Just click the following button in the Options dialog under Text Editor > C# > Code Style > General:

在此输入图像描述

如果您在文本编辑器中创建,您可能需要在文件顶部添加此内容,并根据需要进行调整:

# Remove the line below if you want to inherit .editorconfig settings from higher directories
root = true

# C# files
[*.cs]
Run Code Online (Sandbox Code Playgroud)

在编辑器配置文件中禁用 IDE0044

要专门禁用 IDE0044,请在文件中添加或更改以下设置.editorconfig

dotnet_style_readonly_field = false:none
Run Code Online (Sandbox Code Playgroud)

(在 Visual Studio 2019 中,您可以在“选项”中将该Prefer readonly选项设置为No“下” ,然后按下上面详细介绍的按钮)。TextEditor-> C# -> Code Style-> GeneralGenerate .editorconfig file from settings