Implement IDisposable

This question already has an answer here:

  • Proper use of the IDisposable interface 18 answers

  • Given that it uses explicit interface implementation, it's not clear to me that they want you to, but you can:

    public class MyClass : IDisposable
    {
       private WebServiceHost m_WebServiceHost;
       // Members
       public void Dispose()
       {
                ((IDisposable)m_WebServiceHost).Dispose();
       }
    }
    

    I would guess that they'd prefer you just to call Close() on it, but I can't back that up from documentation yet.


    像这样做:

    public class MyClass : IDisposable
    {
       private WebServiceHost m_WebServiceHost;
    
       // Often you have to override Dispose method 
       protected virtual void Dispose(Boolean disposing) {
         if (disposing) {
           // It looks that WebServiceHost implements IDisposable explicitly
           IDisposable disp = m_WebServiceHost as IDisposable;
    
           if (!Object.RefrenceEquals(null, disp))
             disp.Dispose();
    
           // May be useful when debugging
           disp = null;       
         }
       }
    
       // Members
       public void Dispose()
       {
         Dispose(true);
         GC.SuppressFinalize(this);
       }
    }
    
    链接地址: http://www.djcxy.com/p/54486.html

    上一篇: 如何使用Dispose方法处理对象

    下一篇: 实现IDisposable