代码分析CA1063自IDisposable派生并在基类中提供实现的时候触发 [英] Code Analysis CA1063 fires when deriving from IDisposable and providing implementation in base class

查看:217
本文介绍了代码分析CA1063自IDisposable派生并在基类中提供实现的时候触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,将触发代码分析警告CA1063:

I have some code that will trigger Code Analysis warning CA1063:

CA1063:Microsoft.Design:从接口列表中删除了IDisposable用功能实现并重写的基类处置,而不是执行

CA1063 : Microsoft.Design : Remove IDisposable from the list of interfaces implemented by 'Functionality' and override the base class Dispose implementation instead.

不过,我不知道我需要做的解决这个问题的警告。

However, I'm not sure what I need to do to fix this warning.

简单地说,我有一个接口 IFunctionality IDisposable的<派生/ code>。类功能工具 IFunctionality 但是从类派生可重复使用是能够重用索姆代码。类可重复使用也由的IDisposable 派生的。

Briefly, I have an interface IFunctionality that derives from IDisposable. Class Functionality implements IFunctionality but derives from class Reusable to be able to reuse som code. Class Reusable also derives from IDisposable.

public class Reusable : IDisposable {

  ~Reusable() {
    Dispose(false);
  }

  public void Dispose() {
    Dispose(true);
    GC.SuppressFinalize(this);
  }

  protected virtual void Dispose(Boolean disposing) {
    // ...
  }

  public void DoSomething() {
    // ...
  }

}

public interface IFunctionality : IDisposable {

  void DoSomething();

  void DoSomethingElse();

}

public class Functionality : Reusable, IFunctionality {

  public void DoSomethingElse() {
    // ...
  }

#if WORK_AROUND_CA1063
  // Removes CA1063
  protected override void Dispose(Boolean disposing) {
    base.Dispose(disposing);
  }
#endif

}



我可以得到通过覆盖的Dispose 功能摆脱了警告并调用基类处置,即使这样做,不应该改变代码的语义。

I can get rid of the warning by overriding Dispose on Functionality and calling the base class Dispose even though doing that should not change the semantics of the code .

那么,有没有一些关于的IDisposable 在这种情况下我忽略或只是CA1063是擦枪走火为这个特殊的构造?

So is there something about IDisposable in this context I have overlooked or is it just CA1063 that misfires for this particular construct?

我知道我可以抑制CA1063,但规则是相当广泛和我不想错过在实施任何其他问题的IDisposable 此规则的报道。

I know that I can suppress CA1063 but the rule is quite broad and I don't want to miss any other problems in implementing IDisposable reported by this rule.

推荐答案

这是一个假阳性由于规则本身就是一个小错误。当试图找出如果一个类重新实现IDisposable(搞清楚这有可能被覆盖的基类实施后),它只着眼于类的接口是否包含了IDisposable。不幸的是,在程序集元数据中显示的接口列表中包含的接口爆炸名单,包括通过该类明确实现了原来的C#代码的接口继承任何接口。这意味着,FxCop的是看到的声明看起来像你的功能类以下内容:

This is a false positive due to a minor bug in the rule itself. When trying to figure out if a class re-implements IDisposable (after figuring out that there's a base class implementation that could be overridden), it only looks at whether the class' interfaces include IDisposable. Unfortunately, the interface list that shows up in the assembly metadata includes the "exploded" list of interfaces, including any interfaces inherited via interfaces that the class explicitly implements in the original C# code. This means that FxCop is seeing a declaration that looks like the following for your Functionality class:

public class Functionality : Reusable, IFunctionality, IDisposable
{
    ...
}

鉴于这种元数据代表性,ImplementIDisposableCorrectly规则应该多一点聪明的它是如何尝试确定该类是否实际上是重新实现IDisposable接口(例如,通过寻找一个明确的Dispose()实现如果基类有一个重写的Dispose(布尔) )。然而,由于该规则并没有这样做,你最好的办法是抑制误报。

Given this metadata representation, the ImplementIDisposableCorrectly rule should be a bit more intelligent about how it attempts to determine whether the class is actually re-implementing IDisposable (for example, by looking for an explicit Dispose() implementation if the base class has an overridable Dispose(bool)). However, given that the rule doesn't do this, your best approach is to suppress the false positives.

顺便说一句,我会建议使用的SuppressMessageAttribute 抑制而非目前的条件编译方法的误报。例如:

BTW, I would recommend seriously considering using SuppressMessageAttribute for suppressing the false positives instead of your current conditional compilation approach. e.g.:

[SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly",
    Justification = "False positive.  IDisposable is inherited via IFunctionality.  See http://stackoverflow.com/questions/8925925/code-analysis-ca1063-fires-when-deriving-from-idisposable-and-providing-implemen for details.")]
public class Functionality : Reusable, IFunctionality
{
    ...
}

此外,您可能要认真考虑除暴安良的终结 ...

Also, you might want to seriously consider getting rid of the finalizer...

这篇关于代码分析CA1063自IDisposable派生并在基类中提供实现的时候触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆