无法抑制CA1502

时间:2017-09-02 16:03:25

标签: c# code-analysis maintainability

我有一个类似于这样的课程:

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

这会导致CA1502警告形式&#34; Builder.Builder()的圈复杂度为......&#34; (这种初始化器的已知问题)。但是我的问题是我无法抑制警告。我已经尝试将SuppressMessageAttribute放在代码中的各种不同的地方,但它只是被忽略了。有人建议吗?

2 个答案:

答案 0 :(得分:1)

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

using System.Diagnostics.CodeAnalysis;

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

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

为了将来参考,我通过右键单击Error List窗口(或VS2013及更早版本的Code Analysis窗口)中的CA警告并选择Suppress - &gt;来生成此属性。在抑制文件中。

答案 1 :(得分:-1)

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

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