Visual Studio:抑制名称空间中所有文件的警告

时间:2019-05-16 09:10:55

标签: c# visual-studio roslyn code-analysis roslyn-code-analysis

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

enter image description here

我想在特定名称空间上禁用特定警告(比如说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")]

我尝试将Scopetype更改为namespacenamespaceanddescendants,但是没有用。

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

任何想法如何解决?我正在使用Visual Studio 2017。

3 个答案:

答案 0 :(得分:0)

我认为您的命名空间值错误。看来您的根名称空间是Project,所以您应该尝试:

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

希望这对您有所帮助。

答案 1 :(得分:0)

您不应将~T:用于命名空间,它似乎仅用于类型。作为一个用法示例,您可以在the .NET Core code here中查看它如何不用于命名空间。另外,仅from the docs namespace

  

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

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

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

答案 2 :(得分:0)

您试图禁止对特定命名空间下所有代码元素的特定警告,但是根据Microsoft文档,这是不可能的。它只能应用于特定的代码工件:类,方法/函数,属性等...

在文档中:

  

SuppressMessageAttribute类:禁止报告特定的静态分析工具规则冲突,从而允许对单个代码工件进行多次抑制。

     

请参阅:https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.suppressmessageattribute?view=netframework-4.8

相关问题