using System; namespace SharpGen.Runtime { /// /// Base class for a class. /// public abstract class DisposeBase : IDisposable { /// /// Occurs when this instance is starting to be disposed. /// public event EventHandler Disposing; /// /// Occurs when this instance is fully disposed. /// public event EventHandler Disposed; /// /// Releases unmanaged resources and performs other cleanup operations before the /// is reclaimed by garbage collection. /// ~DisposeBase() { // Finalizer calls Dispose(false) CheckAndDispose(false); } /// /// Gets a value indicating whether this instance is disposed. /// /// /// true if this instance is disposed; otherwise, false. /// public bool IsDisposed { get; private set; } /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// public void Dispose() { CheckAndDispose(true); } /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// private void CheckAndDispose(bool disposing) { if (!IsDisposed) { Disposing?.Invoke(this, DisposeEventArgs.Get(disposing)); Dispose(disposing); GC.SuppressFinalize(this); IsDisposed = true; Disposed?.Invoke(this, DisposeEventArgs.Get(disposing)); } } /// /// Releases unmanaged and - optionally - managed resources /// /// true to release both managed and unmanaged resources; false to release only unmanaged resources. protected abstract void Dispose(bool disposing); } }