问题
I've seen answers showing how to suppress a warning for a specific line of code or for a specific project. I don't want that.
I want to suppress a specific warning for all of my projects.
(If it matters, the warning is IDE0044. And I'm using C#.)
回答1:
A recent update to Visual Studio 2017 (15.7.1) has an option for this now. Under the Tools->Options
menu, select the TextEditor->C#->Code Style->General
tab. Under Field preferences
, there is a Prefer readonly
option. Set that to No
.
There is also an editorconfig
setting you can set if you want to check this preference in along side your code, so others who consume your code don't get the warning, but that has to be done on a per solution basis. The editorconfig
value you would set would be:
dotnet_style_readonly_field = false:none
回答2:
You can use the SuppressMessage attribute present under System.Diagnostics.CodeAnalysis
namespace like
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "args")]
Well as you have edited saying I want to suppress a specific warning for all of my projects
You can't do that for a entire project wise AFAIK. But check the linked post once if that helps
How to suppress code analysis messages for all type members?
回答3:
IDE0044 is "add readonly modifier" so why not just add the modifier?
Warnings are telling you that you're doing something wrong, but the app will compile.
It's best to have zero warnings in an ideal world.
回答4:
You may try to use Directory.Build.props adding NoWarn property for specific warnings. I haven't verified it though.
And as it's said in another answer, it's better to fix the root cause instead of ignoring it.
回答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
:
Disabling IDE0044 in the editor config file
To disable IDE0044 specifically, add or change the following setting in the .editorconfig
file:
dotnet_style_readonly_field = false:none
(In Visual Studio 2019, you can set the Prefer readonly
option to No
under TextEditor-> C# -> Code Style-> General
in Options and then press the Generate .editorconfig file from settings
button as detailed above).
来源:https://stackoverflow.com/questions/50399422/suppress-a-warning-for-all-projects-in-visual-studio