【问题标题】:Get FxCop to suppress warnings for a whole type?让 FxCop 抑制整个类型的警告?
【发布时间】:2011-03-16 07:03:58
【问题描述】:

如何抑制整个类型的 FxCop 警告?

namespace ConsoleApplication1
{     
    public static class Serializer<T>
    {
        public static string Serialize(T obj)
        {
            return string.Empty;
        }

        public static T Deserialize(string str)
        {
            return default(T);
        }
    }

我试过了,但它不适合我:

[assembly: SuppressMessage("Microsoft.Design",
    "CA1000:DoNotDeclareStaticMembersOnGenericTypes", Scope = "Type",
    Target = "ConsoleApplication1.Serializer'1")]

【问题讨论】:

    标签: c# .net code-analysis fxcop suppressmessage


    【解决方案1】:

    很遗憾,这行不通。 FxCop 仅处理针对与检测到的违规相同的目标声明的抑制。如果它发现您的 Serialize 方法存在违规,则唯一会“隐藏”该违规的 SuppressMessage 属性要么是在方法本身上声明的,要么是其 Target 属性标识了该方法的属性。

    如果您想为 Serializer&lt;T&gt; 类中的每个静态方法抑制 CA1000 违规,您需要通过为每个方法创建一个 SuppressMessage 属性来做到这一点。

    @Matt Faus:那么Scope 参数的意义何在?

    Scope 参数让 FxCop 知道Target 参数代表什么类型的东西。例如,如果Target"A.B.C",那么它是指命名空间A.B.C 还是命名空间A.B 中名为C 的类? Scope 可能应该被命名为 TargetKind,但不幸的是,它并没有改变它实际代表的内容......

    另见this answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      • 1970-01-01
      • 2011-12-10
      • 2021-08-28
      • 2021-07-17
      相关资源
      最近更新 更多