在C#中调用Dispose(IsDisposing)模式的目的?[英] Purpose of Dispose calling Dispose(IsDisposing) pattern in C#?

本文是小编为大家收集整理的关于在C#中调用Dispose(IsDisposing)模式的目的?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

这是来自 msdn 的代码.我不明白为什么不仅在此处使用常规dispose方法完成工作.使用(bool)方法的目的是什么?谁会在这里打电话给(false)?

public void Dispose() 
{
    Dispose(true);

    // Use SupressFinalize in case a subclass
    // of this type implements a finalizer.
    GC.SuppressFinalize(this);      
}

protected virtual void Dispose(bool disposing)
{
    // If you need thread safety, use a lock around these 
    // operations, as well as in your methods that use the resource.
    if (!_disposed)
    {
        if (disposing) {
            if (_resource != null)
                _resource.Dispose();
                Console.WriteLine("Object disposed.");
        }

        // Indicate that the instance has been disposed.
        _resource = null;
        _disposed = true;   
    }
}

推荐答案

最终化器将调用Dispose(false) - 在这种情况下,您不会触摸任何其他托管资源(可能已经已经完成了).

就我个人而言,我不经常遵循这种模式 - 因为我很少非常需要最终制度,而且对于我来说,编写非密封的IDisposable实现也很少.如果您在没有最终制度的情况下编写密封班,我会选择一个简单的实现.

其他推荐答案

这是为了允许最终制度工作,并允许从您的班级得出的子类正确处理.

如果您想要更多详细信息,我写了 5 part Blog on Idisposable 和在子类中,详细介绍了子分类问题.

其他推荐答案

关于答案,

您的处置方法(处置)方法如果从最终确定器中调用它,则不应明确免费资源,因为这些资源已经可以被GC释放.

.

它缺少一个重要的词.这应该说:

您的处置方法(处置)方法如果从最终确定器中调用,则不应明确免费最终确定(即托管)资源,因为这些资源可以被GC释放.只能在最终制度中发布本地资源.

我很确定海报的意思是这一点,但在帖子中还不够明确:)

本文地址:https://www.itbaoku.cn/post/627788.html

问题描述

Here is code from MSDN. I don't understand why the work isn't just done in the regular Dispose() method here. What is the purpose of having the Dispose(bool) method? Who would ever call Dispose(false) here?

public void Dispose() 
{
    Dispose(true);

    // Use SupressFinalize in case a subclass
    // of this type implements a finalizer.
    GC.SuppressFinalize(this);      
}

protected virtual void Dispose(bool disposing)
{
    // If you need thread safety, use a lock around these 
    // operations, as well as in your methods that use the resource.
    if (!_disposed)
    {
        if (disposing) {
            if (_resource != null)
                _resource.Dispose();
                Console.WriteLine("Object disposed.");
        }

        // Indicate that the instance has been disposed.
        _resource = null;
        _disposed = true;   
    }
}

推荐答案

The finalizer would call Dispose(false) - in which case you don't touch any of the other managed resources (which may already have been finalized).

Personally I don't follow this pattern often - because I only very, very rarely need a finalizer, and it's also rare for me to write a non-sealed IDisposable implementation. If you're writing a sealed class without a finalizer, I would go for a simple implementation.

其他推荐答案

This is to allow the finalizer to work property, as well as to allow subclasses which derive from your class to dispose properly.

If you want more detailed info, I wrote a 5 part blog series on IDisposable, and covered the subclassing issue in detail in the Subclass from an IDisposable Class article.

其他推荐答案

Regarding the answer,

Your Dispose(disposing) method shouldn't explicitly free resources if it is called from finalizer, since these resources can be already freed by GC.

It's missing an important word. This should really say:

Your Dispose(disposing) method shouldn't explicitly free finalizable (i.e. managed) resources if it is called from finalizer, since these resources can be already freed by GC. Only native resources should be released in a Finalizer.

I'm pretty sure that the poster meant this but just wasn't explicit enough in the post : )