I have a class which starts off something like this:
namespace Tools.Builders
{
internal abstract class Builder
{
[SuppressMessage("Microsoft.Maintainability", "CA1502")]
private static readonly Dictionary<string, Func<ILogger, Builder>> _builders =
new Dictionary<string, Func<ILogger, Builder>>
{
{ "1", (x) => {return new BuilderType1(x);} },
{ "2", (x) => {return new BuilderType2(x);} },
{ "3", (x) => {return new BuilderType3(x);} },
{ "4", (x) => {return new BuilderType4(x);} },
{ "5", (x) => {return new BuilderType5(x);} },
{ "6", (x) => {return new BuilderType6(x);} },
{ "7", (x) => {return new BuilderType7(x);} },
{ "8", (x) => {return new BuilderType8(x);} },
{ "9", (x) => {return new BuilderType9(x);} },
};
protected ILogger _logger;
protected Builder(ILogger logger)
{
_logger = logger;
}
//...
This causes a CA1502 warning of the form "Builder.Builder() has a cyclomatic complexity of..." (which is a known problem with this sort of initialiser). However my problem is I can't suppress the warning. I've tried putting the SuppressMessageAttribute in all sorts of different places in the code, but it just gets ignored. Any suggestions anyone?
I was able to suppress this message by using an assembly-level attribute that specifies the constructor as the target:
using System.Diagnostics.CodeAnalysis;
[assembly: SuppressMessage("Microsoft.Maintainability",
"CA1502:AvoidExcessiveComplexity",
Scope = "member",
Target = "Tools.Builders.Builder.#.cctor()")]
This attribute can be placed in any code file in the assembly.
For future reference, I generated this attribute by right-clicking the CA warning in the Error List window (or the Code Analysis window in VS2013 and earlier) and selecting Suppress -> In Suppression File.