Let's say I have an enumeration of all currencies:
public enum CurrencyType
{
/// <summary>
/// United Arab Emirates dirham
/// </summary>
[EnumMember]
AED = 784,
/// <summary>
/// Afghan afghani
/// </summary>
[EnumMember]
AFN = 971,
/// <summary>
/// Albanian lek
/// </summary>
[EnumMember]
ALL = 008,
...
}
VS 2015 code analysis keeps complaining about 100 violations of CA1709 for every individual member.
This is an useful rule by itself, and I do not want to disable it; yet it is of not much help in this specific case, as CurrencyType
is public and is used in a whole lot of other projects.
I can suppress the message; however, VS only offers me to suppress it for every individual member - meaning that I'll have 100 [SuppressMessage(...)]
lines, which will clutter the code.
Is there any way to suppress all CA1709 for all CurrencyType
members, while not suppressing it for all other code in this project, without having to write 100 [SuppressMessage(...)]
?
There is a Scope
parameter of SuppressMessageAttribute
, but the documentation is unclear on that one. I've tried placing both
[SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", Scope = "type", Justification = "Currency codes are defined in ISO standard.")]
and
[SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", Scope = "member", Justification = "Currency codes are defined in ISO standard.")]
on CurrencyType
itself. Neither does work.
You can suppress violations in code using a preprocessor directive, the #pragma warning (C#) or Disable (Visual Basic) directive to suppress the warning for only a specific line of code. Or, you can use the SuppressMessage attribute.
Use of @SuppressWarnings is to suppress or ignore warnings coming from the compiler, i.e., the compiler will ignore warnings if any for that piece of code. 1. @SuppressWarnings("unchecked") public class Calculator { } - Here, it will ignore all unchecked warnings coming from that class.
To disable a set of warnings for a given piece of code, you have to start with a “push” pre-processor instruction, then with a disabling instruction for each of the warning you want to suppress, and finish with a “pop” pre-processor instruction.
There is no way to suppress a rule for a whole class or enum in this case and have the suppression apply to all of its members, unfortunately.
But what you can do, is create a CodeAnalaysisDictionary.xml
, add it to your project containing the Enum and setting its 'Build action' propery to CodeAnalysisDictionary
:
Once you have set this up, you can add the abbreviations and case exceptions to the dictionary like this:
<Dictionary>
<Acronyms>
<CasingExceptions>
<Acronym>AED</Acronym>
<Acronym>AFN</Acronym>
<Acronym>ALL</Acronym>
<Acronym>...</Acronym>
</CasingExceptions>
</Acronyms>
</Dictionary>
While these exceptions will apply to any element in the code with these acronyms in them, they will prevent the CA1709 warnings from showing up.
See the documentation for more information on the exceptions you can setup using the dictionary files:
No, there is no way to do this without individual suppressions. The Scope
argument lets the code analysis engine know what kind of thing the Target
argument represents. For example, if the Target is "A.B.C", does that refer to a namespace named A.B.C
or a class named C
in the namespace A.B
? "Scope" would perhaps have been better represented by a name like "TargetKind", but that, unfortunately, does not change what it actually represents.
Given the ugliness of the suppressions in this case, you might want to generate them into GlobalSuppressions.cs
, then move them into a separate file like CurrencyTypeMemberNameSuppressions.cs
, which you could (optionally) nest as a file under the file containing your CurrencyType
enum in your project structure in Visual Studio. Not ideal, but maybe the best choice of a bad lot at this point...
Also see this answer.
what about #pragma warning disable CA1709
?
to reactivate you can use #pragma warning restore CA1709
but if this enum is the only one type in your file you can leave that out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With