如何正确实现带有 close 方法的处置模式(CA1063)

发布于 2024-07-14 14:51:23 字数 1988 浏览 3 评论 0原文

框架设计指南(第二版,第 327 页)说:

如果 close 是该领域的标准术语,除了 Dispose() 之外,请考虑提供方法 Close()

这样做时,重要的是使 Close 实现与 Dispose 相同,并考虑显式实现 IDisposable.Dispose 方法。

因此,按照提供的示例,我得到了此类:

public class SomeClass : IDisposable {
    private SomeDisposable someInnerDisposable;

    public void Open() {
        this.someInnerDisposable = new SomeDisposable();
    }

    void IDisposable.Dispose() {
        this.Close();
    }

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

    protected virtual void Dispose(bool disposing) {
        if (disposing) {
            this.someInnerDisposable.Dispose();
            this.someInnerDisposable = null;
        }
    }
}

FxCop 似乎不喜欢这样:

CA1816:Microsoft.Usage:“SomeClass.Close()”调用“GC.SuppressFinalize(object)”,该方法通常仅在“IDisposable.Dispose”的实现中调用。 有关详细信息,请参阅 IDisposable 模式。

CA1816:Microsoft.Usage:将“SomeClass.IDisposable.Dispose()”更改为调用“GC.SuppressFinalize(object)”。 一旦对象被处置并且超出范围,这将防止对象不必要的终结。

CA1063:Microsoft.Design:修改“SomeClass.IDisposable.Dispose()”,以便它调用 Dispose(true),然后对当前对象实例(Visual Basic 中的“this”或“Me”)调用 GC.SuppressFinalize ,然后返回。

CA1063:Microsoft.Design:将“SomeClass.IDisposable.Dispose()”重命名为“Dispose”,并确保将其声明为公共并密封。

  • 如何正确实现带有 close 方法的处置模式?

- 或 -

  • 如何抑制警告?

我尝试过

[SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly",
    Justification = "Framework Design Guidelines say it's ok.")]
void IDisposable.Dispose()
{
    this.Close();
}

,但 FxCop 1.36 仍然报告它们。

编辑:按照建议进行更改可以消除除此警告之外的所有警告:

CA1063:Microsoft.Design:将“SomeClass.IDisposable.Dispose()”重命名为“Dispose”,并确保将其声明为公共并密封。

编辑2:CODE_ANALYSIS确实丢失了。 谢谢。

The Framework Design Guidelines (2nd Ed., page 327) say:

CONSIDER providing method Close(), in addition to the Dispose(), if close is standard terminology in the area.

When doing so, it is important that you make the Close implementation identical to Dispose and consider implementing IDisposable.Dispose method explicitly.

So, following the provided example, I've got this class:

public class SomeClass : IDisposable {
    private SomeDisposable someInnerDisposable;

    public void Open() {
        this.someInnerDisposable = new SomeDisposable();
    }

    void IDisposable.Dispose() {
        this.Close();
    }

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

    protected virtual void Dispose(bool disposing) {
        if (disposing) {
            this.someInnerDisposable.Dispose();
            this.someInnerDisposable = null;
        }
    }
}

FxCop doesn't seem to like that:

CA1816 : Microsoft.Usage : 'SomeClass.Close()' calls 'GC.SuppressFinalize(object)', a method that is typically only called within an implementation of 'IDisposable.Dispose'. Refer to the IDisposable pattern for more information.

CA1816 : Microsoft.Usage : Change 'SomeClass.IDisposable.Dispose()' to call 'GC.SuppressFinalize(object)'. This will prevent unnecessary finalization of the object once it has been disposed and it has fallen out of scope.

CA1063 : Microsoft.Design : Modify 'SomeClass.IDisposable.Dispose()' so that it calls Dispose(true), then calls GC.SuppressFinalize on the current object instance ('this' or 'Me' in Visual Basic), and then returns.

CA1063 : Microsoft.Design : Rename 'SomeClass.IDisposable.Dispose()' to 'Dispose' and ensure that it is declared as public and sealed.

  • How do I implement the dispose pattern with close method correctly?

-or-

  • How do I suppress the warnings?

I tried

[SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly",
    Justification = "Framework Design Guidelines say it's ok.")]
void IDisposable.Dispose()
{
    this.Close();
}

but FxCop 1.36 still reports them.

EDIT: Changing it around as suggested eliminates all but this warning:

CA1063 : Microsoft.Design : Rename 'SomeClass.IDisposable.Dispose()' to 'Dispose' and ensure that it is declared as public and sealed.

EDIT 2: CODE_ANALYSIS was indeed missing. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不再见 2024-07-21 14:51:23

改变它。

让 Close() 调用 this.Dispose() 并将逻辑放入 Dispose() 方法而不是 Close() 方法中。

------------------- 编辑后的更多信息 ---------------

另外,将声明更改为:

public void Dispose()

应该删除其他错误。 由于您已将其声明为:

void IDisposable.Dispose()

它未标记为公开且密封,并且 FxCop 抱怨道。 就我个人而言,我更愿意避免错误而不是抑制错误。

Change it around.

Have Close() call this.Dispose() and put the logic in the Dispose() method instead of the Close() method.

------------------- Further info after edit ---------------

Also, changing the declaration to:

public void Dispose()

should get rid of the other error. Since you have it declared as:

void IDisposable.Dispose()

It's not marked as public and sealed, and FxCop complains. Personally, I prefer to get avoid the errors instead of suppressing them.

折戟 2024-07-21 14:51:23

如何抑制警告?

仅当您将 CODE_ANALYSIS 标志指定为条件编译符号时,SuppressMessage() 才起作用。

How do I suppress the warnings?

SuppressMessage() only works when you specify the CODE_ANALYSIS flag as a conditional compilation symbol.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文