From 6d250c4050ceb0bda33aad6a484fd05e508c4e27 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 4 Nov 2016 04:31:05 -0400 Subject: make dlna project portable --- .../Net/DisposableManagedObjectBase.cs | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 Emby.Common.Implementations/Net/DisposableManagedObjectBase.cs (limited to 'Emby.Common.Implementations/Net/DisposableManagedObjectBase.cs') diff --git a/Emby.Common.Implementations/Net/DisposableManagedObjectBase.cs b/Emby.Common.Implementations/Net/DisposableManagedObjectBase.cs new file mode 100644 index 000000000..8476cea32 --- /dev/null +++ b/Emby.Common.Implementations/Net/DisposableManagedObjectBase.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Emby.Common.Implementations.Net +{ + /// + /// Correclty implements the interface and pattern for an object containing only managed resources, and adds a few common niceities not on the interface such as an property. + /// + public abstract class DisposableManagedObjectBase : IDisposable + { + + #region Public Methods + + /// + /// Override this method and dispose any objects you own the lifetime of if disposing is true; + /// + /// True if managed objects should be disposed, if false, only unmanaged resources should be released. + protected abstract void Dispose(bool disposing); + + /// + /// Throws and if the property is true. + /// + /// + /// Thrown if the property is true. + /// + protected virtual void ThrowIfDisposed() + { + if (this.IsDisposed) throw new ObjectDisposedException(this.GetType().FullName); + } + + #endregion + + #region Public Properties + + /// + /// Sets or returns a boolean indicating whether or not this instance has been disposed. + /// + /// + public bool IsDisposed + { + get; + private set; + } + + #endregion + + #region IDisposable Members + + /// + /// Disposes this object instance and all internally managed resources. + /// + /// + /// Sets the property to true. Does not explicitly throw an exception if called multiple times, but makes no promises about behaviour of derived classes. + /// + /// + public void Dispose() + { + try + { + IsDisposed = true; + + Dispose(true); + } + finally + { + GC.SuppressFinalize(this); + } + } + + #endregion + } +} -- cgit v1.2.3