无法抑制 CA1502

Can't suppress CA1502

我有一个 class 开始是这样的:

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;
        }
        //...

这会导致 "Builder.Builder() has a cyclomatic complexity of..." 形式的 CA1502 警告(这是此类初始化程序的已知问题)。但是我的问题是我无法抑制警告。我试过将 SuppressMessageAttribute 放在代码中的各种不同位置,但它只是被忽略了。有人有什么建议吗?

将以下属性应用于我在 VS 2017 中使用的相关方法

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]

我能够通过使用将构造函数指定为目标的程序集级属性来抑制此消息:

using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage("Microsoft.Maintainability",
                           "CA1502:AvoidExcessiveComplexity",
                           Scope = "member",
                           Target = "Tools.Builders.Builder.#.cctor()")]

该属性可以放在程序集中的任何代码文件中。

为了将来参考,我通过右键单击错误列表 window 中的 CA 警告(或 VS2013 及更早版本中的代码分析 window)并选择抑制 -> 在抑制文件。