标准的Dispose模式?为什么我们需要与QUOT;处理"参数的虚方法和心不是在fianlizer dipose后总是被调用?是在、参数、模式、标准

2023-09-04 23:47:41 作者:就爱你的小任性

为什么我们需要参数处理在下面的code段。

此外,我们调用处理与假,在终结,它不会释放或做清理。

那么,如果处置永远不会叫什么名字?

时不会出售总要得到终结前,叫什么名字?

 使用系统;
公共类MyClass的:IDisposable的
{
    私人布尔处置= FALSE;
    受保护的虚拟无效的Dispose(BOOL处置)
    {
        如果(!处置)
        {
            ** //我们真的需要这个条件?
            如果(处置)**
            {
                //通过myClass.Dispose称为()。
                // OK使用任何私有对象引用
            }
            处置= TRUE;
        }
    }
    公共无效的Dispose()
        //实现IDisposable
    {
        处置(真);
        GC.Sup pressFinalize(本);
    }
    〜MyClass的()//终结
    {
        //为什么我们需要用虚假的称呼?
        处置(假);
    }
}
 

在换句话说,为什么不?

 使用系统;
公共类MyClass的:IDisposable的
{
    私人布尔处置= FALSE;
    受保护的虚拟无效的Dispose(布尔燮pressFinalize)
    {
        如果(!处置)
        {
            //我们真的需要这个条件?

                //通过myClass.Dispose称为()。
                // OK使用任何私有对象引用
            处置= TRUE;
        }
        如果(!SUP pressFinalize)
        {
            GC.Sup pressFinalize(本);
        }
    }
    公共无效的Dispose()
        //实现IDisposable
    {
        处置(真);

    }
    〜MyClass的()//终结
    {
        //为什么我们需要用虚假的称呼?
        处置(假);
    }
}
 

其实,我真的需要终结?为什么不呢?

 使用系统;
公共类MyClass的:IDisposable的
{
    公共无效的Dispose()
        //实现IDisposable
    {
        //只是做了清理和释放资源
        GC.Sup pressFinalize(本);
    }

}
 
6.事件系统

感谢您, 梦想家

解决方案   

此外,我们调用处理与假,在终结,它不会释放或做清理。

实际上 - 它会假设其他类处理在这种情况下,自己的清理工作,只有做好清理直接的非托管资源

  

那么,如果处置永远不会叫?

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

  

时不会出售总要得到终结前,叫什么名字?

若没有人称之为无论出于何种原因。

我觉得这个模式比它需要的,因为它试图解释类,它们作为基类为其他类,它们的更复杂的可能的需要终结。封你的类,你可以只实现正是你希望是什么:)

您可能还需要阅读乔·达菲的"Never再写一个终结博客文章和long解释模式的。

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

Moreover we invoke dispose with false, in finalizer and it wont 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);
    } 

} 

Thank you, Dreamer

解决方案

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.

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 :)

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