DisposableManagedObjectBase.cs 3.0 KB

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