【问题标题】:How do I suppress CA1725?如何抑制 CA1725?
【发布时间】:2014-04-12 15:13:13
【问题描述】:

我有什么

我想实现一个 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 对这个宝石抱怨:

将方法 MyBackgroundWorker.MyNamespace.IBackgroundWorker.set_WorkerSupportsCancellation(Boolean)Void 的参数名称“param0”更改为“值”,以便与 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")]

【问题讨论】:

  • WorkerSupportsCancellation 的定义在哪里?
  • 我很抱歉 - 我已经更正了我的示例以反映我目前的实际情况,而不是一些中期编辑混合。 WorkerSupportsCancellation 属性在 BackgroundWorkerIBackgroundWorker 中定义,具有相同的签名(转到每个中的定义显示相同的内容 - bool WorkerSupportsCancellation { get; set; })。
  • 您使用的是哪个版本的 VS 以及哪个规则集,因为我无法使用您当前的代码示例重现此警告。
  • 我知道它是VS2012,我知道输出包括FxCopCmd,但是我找不到规则集的设置位置。项目中<RunCodeAnalysis>为false,<DefineConstants>中设置CODE_ANALYSISFxCopCmd.exe.config 文件是否有助于回答您的问题?
  • 您可以尝试使用MyBackgroundWorker 的新修饰符来提升WorkerSupportsCancellation。这不会在正常工具链中为我生成(新)警告,但 Resharper 确实检测到该构造的错误。

标签: c# .net fxcop


【解决方案1】:

How do I disable a specific FxCop rule in generated code? 有答案 - 我可以使用 Copy As -> Module-level SuppressMessage 从 GUI 工具中获取完全限定名称(我是对的,这是我无法正确理解的部分),并且它有效。

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

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

【讨论】:

    猜你喜欢
    • 2016-12-05
    • 1970-01-01
    • 2017-04-25
    • 2015-05-20
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 2014-10-17
    相关资源
    最近更新 更多