2
0

DisposableManagedObjectBase.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. namespace Emby.Server.Implementations.Net
  3. {
  4. /// <summary>
  5. /// 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.
  6. /// </summary>
  7. public abstract class DisposableManagedObjectBase : IDisposable
  8. {
  9. #region Public Methods
  10. /// <summary>
  11. /// Override this method and dispose any objects you own the lifetime of if disposing is true;
  12. /// </summary>
  13. /// <param name="disposing">True if managed objects should be disposed, if false, only unmanaged resources should be released.</param>
  14. protected abstract void Dispose(bool disposing);
  15. /// <summary>
  16. /// Throws and <see cref="System.ObjectDisposedException"/> if the <see cref="IsDisposed"/> property is true.
  17. /// </summary>
  18. /// <seealso cref="IsDisposed"/>
  19. /// <exception cref="System.ObjectDisposedException">Thrown if the <see cref="IsDisposed"/> property is true.</exception>
  20. /// <seealso cref="Dispose()"/>
  21. protected virtual void ThrowIfDisposed()
  22. {
  23. if (this.IsDisposed) throw new ObjectDisposedException(this.GetType().FullName);
  24. }
  25. #endregion
  26. #region Public Properties
  27. /// <summary>
  28. /// Sets or returns a boolean indicating whether or not this instance has been disposed.
  29. /// </summary>
  30. /// <seealso cref="Dispose()"/>
  31. public bool IsDisposed
  32. {
  33. get;
  34. private set;
  35. }
  36. #endregion
  37. #region IDisposable Members
  38. /// <summary>
  39. /// Disposes this object instance and all internally managed resources.
  40. /// </summary>
  41. /// <remarks>
  42. /// <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>
  43. /// </remarks>
  44. /// <seealso cref="IsDisposed"/>
  45. public void Dispose()
  46. {
  47. IsDisposed = true;
  48. Dispose(true);
  49. }
  50. #endregion
  51. }
  52. }