实现IDisposable的代码片段
1 ~DemoType() 2 { 3 this.Dispose(); 4 } 5 6 #region IDisposable Members 7 8 /// <summary> 9 /// Internal variable which checks if Dispose has already been called 10 /// </summary> 11 protected Boolean disposed; 12 13 /// <summary> 14 /// Releases unmanaged and - optionally - managed resources 15 /// </summary> 16 /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> 17 protected void Dispose(Boolean disposing) 18 { 19 if (disposed) 20 { 21 return; 22 } 23 24 if (disposing) 25 { 26 //TODO: Managed cleanup code here, while managed refs still valid 27 } 28 //TODO: Unmanaged cleanup code here 29 30 disposed = true; 31 } 32 33 /// <summary> 34 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. 35 /// </summary> 36 public void Dispose() 37 { 38 // Call the private Dispose(bool) helper and indicate 39 // that we are explicitly disposing 40 this.Dispose(true); 41 42 // Tell the garbage collector that the object doesn‘t require any 43 // cleanup when collected since Dispose was called explicitly. 44 GC.SuppressFinalize(this); 45 } 46 47 #endregion
时间: 2024-10-13 23:34:50