如何抑制CA1725?

时间:2014-04-12 15:13:14

标签: c# .net fxcop

我拥有什么

我想实现BackgroundWorker的同步替换,以便在我的NUnit测试中使用。根据我的研究,我目前有这个:

    // Interface:
    public interface IBackgroundWorker
    {
        // The properties, events and methods from BackgroundWorker go here
    }
    public delegate IBackgroundWorker BackgroundWorkerFactory();
    // Implementation 1:
    public class MyBackgroundWorker : BackgroundWorker, IBackgroundWorker
    {
        private MyBackgroundWorker() { }
        public static MyBackgroundWorker Factory()
            { return new MyBackgroundWorker(); }
    }
    // Implementation 2 is a synchronous version 

但是,FxCop对这个宝石抱怨:

  

更改参数名称' param0'方法MyBackgroundWorker.MyNamespace.IBackgroundWorker.set_WorkerSupportsCancellation(Boolean)Void to' value'为了匹配在IBackgroundWorker.set_WorkerSupportsCancellation(Boolean)Void中声明的标识符。

(它说的类似于RunWorkerAsync(object),因为它是接口上唯一采用参数的方法)

This MSDN page说我不应该压制消息,但是this说我应该,因为它只是编译器生成的问题而且FxCop版本1.36修复了它

我想要什么

我想使用这个瘦包装器而不是需要将每个方法和属性代理到一个封闭的对象(这将是可行的,并且可以避免这个问题,但是作为面向未来的设计并不理想)。

然后我想要禁止来自FxCop 1.35的消息,最好使用模块本身的SuppressMessage属性,因为构建过程超出了我的控制范围(它集中管理)。

我尝试了什么

我根据我在网络上其他地方找到的内容(例如here)尝试了几种SuppressMessage变体,但没有一种具有预期的效果。

[assembly: SuppressMessage("Microsoft.Naming", "CA1725", MessageId = "0#", Scope = "member",
    Target = "MyNamespace.MyBackgroundWorker.#set_WorkerSupportsCancellation(System.Boolean)")]
[assembly: SuppressMessage("Microsoft.Naming", "CA1725", MessageId = "0#", Scope = "member",
    Target = "MyNamespace.MyBackgroundWorker.set_WorkerSupportsCancellation(System.Boolean)")]
[assembly: SuppressMessage("Microsoft.Naming", "CA1725", MessageId = "0#", Scope = "member",
    Target = "MyNamespace.IBackgroundWorker.#set_WorkerSupportsCancellation(System.Boolean)")]
[assembly: SuppressMessage("Microsoft.Naming", "CA1725", MessageId = "0#", Scope = "member",
    Target = "MyNamespace.IBackgroundWorker.set_WorkerSupportsCancellation(System.Boolean)")]

[assembly: SuppressMessage("Microsoft.Naming", "CA1725", MessageId = "0#", Scope = "member",
    Target = "MyNamespace.MyBackgroundWorker.#set_WorkerSupportsCancellation(System.Boolean)System.Void")]
[assembly: SuppressMessage("Microsoft.Naming", "CA1725", MessageId = "0#", Scope = "member",
    Target = "MyNamespace.MyBackgroundWorker.set_WorkerSupportsCancellation(System.Boolean)System.Void")]
[assembly: SuppressMessage("Microsoft.Naming", "CA1725", MessageId = "0#", Scope = "member",
    Target = "MyNamespace.IBackgroundWorker.#set_WorkerSupportsCancellation(System.Boolean)System.Void")]
[assembly: SuppressMessage("Microsoft.Naming", "CA1725", MessageId = "0#", Scope = "member",
    Target = "MyNamespace.IBackgroundWorker.set_WorkerSupportsCancellation(System.Boolean)System.Void")]

1 个答案:

答案 0 :(得分:2)

How do I disable a specific FxCop rule in generated code?有答案 - 我可以使用Copy As - >从GUI工具获得完全合格的名称(我说得对,这是我无法做到的)。模块级SuppressMessage,它可以工作。

在我的情况下,我需要指定

[module: SuppressMessage("Microsoft.Naming",
                         "CA1725",
                         MessageId = "0#",
                         Scope = "member",
                         Target = "MyNamespace.MyBackgroundWorker.MyNamespace.IBackgroundWorker.set_WorkerSupportsCancellation(System.Boolean):System.Void")]