Search code examples
c#.netfxcop

How do I suppress CA1725?


What I have

I want to implement a synchronous replacement for a BackgroundWorker, to use in my NUnit testing. From my research I currently have this:

    // 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 

However, FxCop whinges with this gem:

Change parameter name 'param0' of method MyBackgroundWorker.MyNamespace.IBackgroundWorker.set_WorkerSupportsCancellation(Boolean)Void to 'value' in order to match the identifier as it has been declared in IBackgroundWorker.set_WorkerSupportsCancellation(Boolean)Void.

(It says something similar about RunWorkerAsync(object), as that's the only other method on the interface which takes a parameter)

This MSDN page says I shouldn't suppress the message, but this says that I should, as it's simply a compiler-generated problem and a version 1.36 of FxCop fixes it.

What I want

I want to use this thin wrapper rather than needing to proxy each method and property through to an enclosed object (which would be do-able, and would avoid the problem, but is not ideal as a future-proofed design).

I then want to suppress the message from FxCop 1.35, preferably using a SuppressMessage attribute in the module itself, as the build process is outside of my control (it's managed centrally).

What I've tried

I've tried several variants of the SuppressMessage, based on what I've found elsewhere on the web, such as here, but none has the desired effect.

[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")]

Solution

  • How do I disable a specific FxCop rule in generated code? has the answer - I can get the fully-qualified name (I was right about that being the bit I couldn't get right) from the GUI tool using Copy As -> Module-level SuppressMessage, and it works.

    In my case, I need to specify

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