首页 > 解决方案 > Visual Studio:抑制命名空间中所有文件的警告

问题描述

我的项目中有以下命名空间。

在此处输入图像描述

我想禁用特定命名空间上的特定警告(比如说 Project.ViewModels)。我可以通过在GlobalSuppression.cs

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Formatting", "RCS1057:Add empty line between declarations.", Justification = "<Pending>", Scope = "type", Target = "~T:Project.ViewModels.MainViewModel.cs")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Formatting", "RCS1057:Add empty line between declarations.", Justification = "<Pending>", Scope = "type", Target = "~T:Project.ViewModels.TreeViewModel.cs")]

我试图Scopetypeto更改namespacenamespaceanddescendants但它没有工作。

[assembly: SuppressMessage("Formatting", "RCS1057:Add empty line between declarations.", Justification = "<Pending>", Scope = "namespace", Target = "~T:Project.ViewModels")]

知道如何解决这个问题吗?我正在使用 Visual Studio 2017。

标签: c#visual-studioroslyncode-analysisroslyn-code-analysis

解决方案


您不应该将~T:用于命名空间,这似乎仅适用于类型。对于一个使用示例,您可以在此处查看 .NET Core 代码中的命名空间如何不使用它。此外,仅来自文档 namespace

禁止对命名空间本身发出警告。它不会抑制针对命名空间内的类型的警告,如下所示:

根据您的文件层次结构,您可能希望使用namespaceanddescendants如下所示:

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Formatting", "RCS1057:Add empty line between declarations.", Justification = "<Pending>", Scope = "namespaceanddescendants", Target = "Project.ViewModels")]

推荐阅读