DisposableManagedObjectBase.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. namespace Rssdp.Infrastructure
  6. {
  7. /// <summary>
  8. /// 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.
  9. /// </summary>
  10. public abstract class DisposableManagedObjectBase : IDisposable
  11. {
  12. #region Public Methods
  13. /// <summary>
  14. /// Override this method and dispose any objects you own the lifetime of if disposing is true;
  15. /// </summary>
  16. /// <param name="disposing">True if managed objects should be disposed, if false, only unmanaged resources should be released.</param>
  17. protected abstract void Dispose(bool disposing);
  18. /// <summary>
  19. /// Throws and <see cref="ObjectDisposedException"/> if the <see cref="IsDisposed"/> property is true.
  20. /// </summary>
  21. /// <seealso cref="IsDisposed"/>
  22. /// <exception cref="ObjectDisposedException">Thrown if the <see cref="IsDisposed"/> property is true.</exception>
  23. /// <seealso cref="Dispose()"/>
  24. protected virtual void ThrowIfDisposed()
  25. {
  26. if (this.IsDisposed) throw new ObjectDisposedException(this.GetType().FullName);
  27. }
  28. #endregion
  29. #region Public Properties
  30. /// <summary>
  31. /// Sets or returns a boolean indicating whether or not this instance has been disposed.
  32. /// </summary>
  33. /// <seealso cref="Dispose()"/>
  34. public bool IsDisposed
  35. {
  36. get;
  37. private set;
  38. }
  39. #endregion
  40. public string BuildMessage(string header, Dictionary<string, string> values)
  41. {
  42. var builder = new StringBuilder();
  43. const string argFormat = "{0}: {1}\r\n";
  44. builder.AppendFormat("{0}\r\n", header);
  45. foreach (var pair in values)
  46. {
  47. builder.AppendFormat(argFormat, pair.Key, pair.Value);
  48. }
  49. builder.Append("\r\n");
  50. return builder.ToString();
  51. }
  52. #region IDisposable Members
  53. /// <summary>
  54. /// Disposes this object instance and all internally managed resources.
  55. /// </summary>
  56. /// <remarks>
  57. /// <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>
  58. /// </remarks>
  59. /// <seealso cref="IsDisposed"/>
  60. [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.")]
  61. public void Dispose()
  62. {
  63. IsDisposed = true;
  64. Dispose(true);
  65. }
  66. #endregion
  67. }
  68. }