标准配置模式?为什么我们需要“处置”?虚方法中的参数,并且总处置后不调用终结器吗? [英] standard dispose pattern? why do we need "disposing" parameter in the virtual method and isn't the finalizer get called after dispose always?

查看:69
本文介绍了标准配置模式?为什么我们需要“处置”?虚方法中的参数,并且总处置后不调用终结器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在下面的代码片段中需要参数 dispose

Why do we need parameter disposing in the below code snippet.

此外,我们在终结器中使用false调用dispose,它不会释放或进行清理。

Moreover we invoke dispose with false, in finalizer and it won't release or do the clean up.

那么,如果处置从未被召唤怎么办?

So what if dispose never get called?

处置不是总是在终结器之前被调用吗?

Isn't dispose always get called before finalizer?

using System;
public class MyClass : IDisposable 
{ 
    private bool disposed = false;  
    protected virtual void Dispose(bool disposing) 
    {    
        if (!disposed)
        {
            **//Do we really need this condition?
            if (disposing)**
            { 
                // called via myClass.Dispose().    
                // OK to use any private object references
            }
            disposed = true; 
        } 
    }
    public void Dispose() 
        // Implement IDisposable     
    {
        Dispose(true);   
        GC.SuppressFinalize(this); 
    } 
    ~MyClass() // the finalizer
    {
        //why do we need to call with false?
        Dispose(false);    
    }
} 

换句话说,为什么不呢?

In other words, why not?

using System;
public class MyClass : IDisposable 
{ 
    private bool disposed = false;  
    protected virtual void Dispose(bool suppressFinalize) 
    {    
        if (!disposed)
        {
            //Do we really need this condition?

                // called via myClass.Dispose().    
                // OK to use any private object references            
            disposed = true; 
        }
        if (!suppressFinalize)
        {
            GC.SuppressFinalize(this); 
        }
    }
    public void Dispose() 
        // Implement IDisposable     
    {
        Dispose(true);   

    } 
    ~MyClass() // the finalizer
    {
        //why do we need to call with false?
        Dispose(false);    
    }
} 

实际上,我真的需要终结器吗?为什么不这样做?

In fact, do I really need finalizer? Why not this?

using System;
public class MyClass : IDisposable 
{     
    public void Dispose() 
        // Implement IDisposable     
    {
        //just do the cleanup and release resources
        GC.SuppressFinalize(this);
    } 

} 


推荐答案


此外,我们在终结器中使用false调用dispose,它将不会释放或进行清理。

Moreover we invoke dispose with false, in finalizer and it wont release or do the clean up.

的确如此-它将假定其他类在这种情况下也进行自己的清理,并且仅对直接的非托管资源进行清理。

Indeed - it will assume that other classes handle their own cleanup in that case, and only do the clean-up of direct unmanaged resources.


那么如果永远都不会被废弃处理呢?

So what if dispose never get called?

然后将调用终结器 ,它将清理所有直接非托管资源,但不必担心间接资源。

Then the finalizer will be called, and it will clean up any direct unmanaged resources, but not worry about indirect resources.


Isn't dispose always get called before finalizer?

如果没有人出于某种原因调用它,那不是吗?

Not if no-one calls it for whatever reason.

我认为这种模式比它需要的更加复杂,因为它试图考虑用作其他可能需要fin的其他类的基类的类施肥者。密封您的课程,您可以完全实现您所期望的:)

I think this pattern is more complicated than it needs to be, as it's trying to account for classes which act as base classes for other classes which might need finalizers. Seal your classes and you can just implement exactly what you'd expect to :)

您可能还想阅读Joe Duffy的>请勿再写终结器 博客文章和对该模式的详细说明

You might also want to read Joe Duffy's "Never write a finalizer again" blog post and the long explanation of the pattern.

这篇关于标准配置模式?为什么我们需要“处置”?虚方法中的参数,并且总处置后不调用终结器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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