aboutsummaryrefslogtreecommitdiff
path: root/RSSDP/DisposableManagedObjectBase.cs
diff options
context:
space:
mode:
authorLuke Pulverenti <luke.pulverenti@gmail.com>2016-10-29 18:22:20 -0400
committerLuke Pulverenti <luke.pulverenti@gmail.com>2016-10-29 18:22:20 -0400
commitdca78b13411db96366dddfa0d68bb6d36d28ad14 (patch)
tree7d41e670f1cadec0db9e1ed7115e151da891f7ea /RSSDP/DisposableManagedObjectBase.cs
parent597e27d1c6199a40398abb068282711a9cb9db1b (diff)
rework dlna project
Diffstat (limited to 'RSSDP/DisposableManagedObjectBase.cs')
-rw-r--r--RSSDP/DisposableManagedObjectBase.cs76
1 files changed, 76 insertions, 0 deletions
diff --git a/RSSDP/DisposableManagedObjectBase.cs b/RSSDP/DisposableManagedObjectBase.cs
new file mode 100644
index 000000000..87f2aa71c
--- /dev/null
+++ b/RSSDP/DisposableManagedObjectBase.cs
@@ -0,0 +1,76 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Rssdp.Infrastructure
+{
+ /// <summary>
+ /// Correclty implements the <see cref="IDisposable"/> interface and pattern for an object containing only managed resources, and adds a few common niceities not on the interface such as an <see cref="IsDisposed"/> property.
+ /// </summary>
+ public abstract class DisposableManagedObjectBase : IDisposable
+ {
+
+ #region Public Methods
+
+ /// <summary>
+ /// Override this method and dispose any objects you own the lifetime of if disposing is true;
+ /// </summary>
+ /// <param name="disposing">True if managed objects should be disposed, if false, only unmanaged resources should be released.</param>
+ protected abstract void Dispose(bool disposing);
+
+ /// <summary>
+ /// Throws and <see cref="System.ObjectDisposedException"/> if the <see cref="IsDisposed"/> property is true.
+ /// </summary>
+ /// <seealso cref="IsDisposed"/>
+ /// <exception cref="System.ObjectDisposedException">Thrown if the <see cref="IsDisposed"/> property is true.</exception>
+ /// <seealso cref="Dispose()"/>
+ protected virtual void ThrowIfDisposed()
+ {
+ if (this.IsDisposed) throw new ObjectDisposedException(this.GetType().FullName);
+ }
+
+ #endregion
+
+ #region Public Properties
+
+ /// <summary>
+ /// Sets or returns a boolean indicating whether or not this instance has been disposed.
+ /// </summary>
+ /// <seealso cref="Dispose()"/>
+ public bool IsDisposed
+ {
+ get;
+ private set;
+ }
+
+ #endregion
+
+ #region IDisposable Members
+
+ /// <summary>
+ /// Disposes this object instance and all internally managed resources.
+ /// </summary>
+ /// <remarks>
+ /// <para>Sets the <see cref="IsDisposed"/> property to true. Does not explicitly throw an exception if called multiple times, but makes no promises about behaviour of derived classes.</para>
+ /// </remarks>
+ /// <seealso cref="IsDisposed"/>
+ [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly", Justification="We do exactly as asked, but CA doesn't seem to like us also setting the IsDisposed property. Too bad, it's a good idea and shouldn't cause an exception or anything likely to interfer with the dispose process.")]
+ public void Dispose()
+ {
+ try
+ {
+ IsDisposed = true;
+
+ Dispose(true);
+ }
+ finally
+ {
+ GC.SuppressFinalize(this);
+ }
+ }
+
+ #endregion
+ }
+} \ No newline at end of file