整个命名空间的'SuppressMessage'

tim*_*use 22 .net c# code-analysis fxcop namespaces

我为我的测试方法使用下划线以获得更好的可读性,并且我希望抑制整个测试命名空间的FxCop错误/警告.

我怎样才能做到这一点?我玩过GlobalSuppressions.cs但没有任何效果:

[module: System.Diagnostics.CodeAnalysis.SuppressMessage(
    "Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores",
    Scope = "namespace", Target = "Company.Product.Tests")]

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage(
    "Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores",
    Scope = "namespace", Target = "Company.Product.Tests")]
Run Code Online (Sandbox Code Playgroud)

car*_*ott 25

您可以为此使用“模块”范围,较旧的编译器比支持较新的“namespaceanddescendants”范围的编译器支持该范围。模块范围影响项目中的所有内容,并且不需要目标规范。

用法示例:

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage(
    "Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores",
    Justification = "Test methods require underscores for readability."
    Scope = "module")]
Run Code Online (Sandbox Code Playgroud)


Len*_*aal 15

自Visual Studio 2019以来,可以抑制名称空间及其所有后代符号的代码分析警告:

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage(
    "Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores",
    Justification = "Test methods require underscores for readability."
    Scope = "namespaceanddescendants", Target = "Company.Product.Tests")]
Run Code Online (Sandbox Code Playgroud)

范围 -禁止显示警告的目标。如果未指定目标,则将其设置为属性的目标。支持的范围包括:

  • ...

  • namespaceanddescendants-(Visual Studio 2019的新增功能)此作用域可抑制名称空间及其所有后代符号中的警告。该namespaceanddescendants值仅对Roslyn分析仪有效,并且被基于FxCop的二进制静态分析忽略。

禁止代码分析警告#SuppressMessage属性@ MS Docs

  • 请注意,您应该添加 ~N: 作为目标的前缀以避免性能问题。参考:https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0077 示例:[程序集:System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Naming", "CA1707 :IdentifiersShouldNotContainUnderscores", Justification = "测试方法需要下划线以提高可读性。" Scope = "namespaceanddescendants", Target = "~N:Company.Product.Tests")] (11认同)

rsy*_*rsy 5

通过使用包含以下规则的 .editorconfig 文件,我设法忽略了专门针对测试项目的特定警告(我遵循命名约定,其中这些始终以“Tests.cs”结尾):

[*Tests.cs]
dotnet_diagnostics.CA1707.severity = none
Run Code Online (Sandbox Code Playgroud)

关于我的答案的更多信息在这里