我有開始了這樣的一類:無法抑制CA1502
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警告「Builder.Builder()有一個圈複雜度...」(這是這種初始化程序已知的問題)。然而我的問題是我無法壓制警告。我試着把SuppressMessageAttribute放在代碼中的各種不同的地方,但它只是被忽略。任何建議任何人?
[文檔](https://msdn.microsoft.com/en-us/library/system.diagnostics.codeanalysis.suppressmessageattribute.suppressmessageattribute(v = vs.110).aspx):_「預處理符號」CODE_ANALYSIS 「必須定義這個屬性是有效的,沒有預處理器符號會導致屬性沒有被應用。」_ – CodeCaster
好吧,我試過了,但沒有任何區別。不管怎麼說,還是要謝謝你。 – Dave
請參見[Pragma不適用於警告CA1502](https://social.msdn.microsoft.com/Forums/zh-CN/872d9c97-02b7-4409-9df7-6202ea0820dc/pragma-not-working-for-warning-ca1502 ?forum = vstscode) – buffjape