2014-01-16 146 views
-2

我想在c#中實現一個iDisposable類,但msdn和stackoverflow中的例子不適用於我。在C#中實現IDisposable的正確方法是什麼?

和IM運行在Visual Studio控制檯應用程序框架2012 4.0

我失去了一些使用還是什麼?

public class Foo : IDisposable 
{ 
    public void Dispose() 
    { 
     Dispose(true); 
     GC.SuppressFinalize(this); 
    } 
} 

我下面這個例子(超鏈接不工作對我來說,不知道爲什麼):
http://msdn.microsoft.com/en-us/library/ms244737.aspx
Implementing IDisposable correctly
How to implement IDisposable properly

+0

你缺少了'的Dispose(布爾)'方法,[本條](http://www.codeproject.com/Articles/15360/Implementing-IDisposable-and-the-Dispose-模式-P)應該有所幫助 – TomT

回答

0

我覺得這是標準的模板或多或少:

private bool disposed = false; 

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


    protected virtual void Dispose(bool disposing) 
    { 

     if(!disposed) 
     { 

      if(disposing) 
      { 
       // destroy managed objects here... 
      } 

      disposed = true; 
     } 
    } 


    ~NameOfClass() 
    { 
     Dispose(false); 
    } 
0

標準的方法是使用Dispose Pattern。另見this

class MyClass : IDisposable 
{ 
    private bool alreadyDisposed = false; 

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

    // This is called by GC automatically at some point if public Dispose() has not already been called 
    ~MyClass() 
    { 
     Dispose(false); 
    } 

    // Each derived class should implement this method and conclude it with base.Dispose(disposeManagedResourcesAlso)) call 
    protected virtual void Dispose(bool disposeManagedResourcesAlso) 
    { 
     if (alreadyDisposed) return; 

     if (disposeManagedResourcesAlso) 
     { 
      ...dispose managed resources... 
     } 

     ...dispose unmanaged resources... 

     alreadyDisposed = true; 
    } 
} 
1

您可以在這裏找到很好的解釋Proper use of the IDisposable interface

簡短的例子:

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

private void Dispose(bool disposing) { 
    if (!this.disposed) { 
     if (disposing) { 
      // clean resources here 
     } 
     disposed = true; 
    } 
} 

private bool disposed = false; 
0
public class MyResource: IDisposable 
    { 
     // Pointer to an external unmanaged resource. 
     private IntPtr handle; 
     // Other managed resource this class uses. 
     private Component component = new Component(); 
     // Track whether Dispose has been called. 
     private bool disposed = false; 

     public MyResource(IntPtr handle) 
     { 
      this.handle = handle; 
     } 

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

     protected virtual void Dispose(bool disposing) 
     { 
      if(!this.disposed) 
      { 
       if(disposing) 
       { 
        // Dispose managed resources. 
        component.Dispose(); 
       } 

       CloseHandle(handle); 
       handle = IntPtr.Zero; 

       // Note disposing has been done. 
       disposed = true; 

      } 
     } 

     // Use interop to call the method necessary 
     // to clean up the unmanaged resource. 
     [System.Runtime.InteropServices.DllImport("Kernel32")] 
     private extern static Boolean CloseHandle(IntPtr handle); 

     ~MyResource() 
     { 
      // Do not re-create Dispose clean-up code here. 
      // Calling Dispose(false) is optimal in terms of 
      // readability and maintainability. 
      Dispose(false); 
     } 
    } 

來源IDisposable Interface

0

典型IDisposable實施可能會是李柯認爲:

public class Foo: IDisposable { 
    ... 

    public Boolean IsDisposed { 
    get; 
    protected set; // <- Or even "private set" 
    } 

    protected virtual void Dispose(Boolean disposing) { 
    if (IsDisposed) 
     return; 

    if (disposing) { 
     // Release all acquired resources here 
    } 

    // You can work with structures (not objects!) here 
    // Hardly want you to do anything here... 

    IsDisposed = true; 
    } 

    public Dispose() { 
    Dispose(true); 
    GC.SuppressFinalize(this); 
    } 
} 
相關問題