问题
I use underscores for my test methods for a better readability and I want to suppress FxCop errors/warnings for the whole test namespace.
How can I achieve this? I played with GlobalSuppressions.cs
but nothing worked:
[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")]
回答1:
Suppression of a code analysis warning for a namespace and all its descendant symbols is possible since 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")]
Scope - The target on which the warning is being suppressed. If the target is not specified, it is set to the target of the attribute. Supported scopes include the following:
...
namespaceanddescendants
- (New for Visual Studio 2019) This scope suppresses warnings in a namespace and all its descendant symbols. Thenamespaceanddescendants
value is only valid for Roslyn analyzers, and is ignored by binary, FxCop-based static analysis.
Suppress code analysis warnings#SuppressMessage attribute @ MS Docs
回答2:
I think it is not possible as harlam357 already said.
回答3:
Yes, that's not possible with FxCop <= 10.0.
What you can do, is to disable CA1707 using a custom rules file (maybe just for your test projects).
回答4:
As already said, it is not possible out of the box. Imho, it is intended because suppress have to be done unitarily.
There is a workaround to do this manually through FXCop 10
with the Copy As > Module-level SuppressMessage
functionality.
Cons, you will have to repeat this each time the namespace is modified but as already said, the global suppressions should be isolated.
- Open your assembly in FXCop 10 (System.Xml here) and run analysis
- Select your namespace (System.Xml here)
- Select all violated rules
- Right click and
Copy As > Module-level SupressMessage
- Then paste it in a
GlobalSuppressions.cs
for example
Note: this can be done at assembly, namespace or type level.

来源:https://stackoverflow.com/questions/11359652/suppressmessage-for-a-whole-namespace