DisposableManagedObjectBase.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. /// <summary>
  12. /// Override this method and dispose any objects you own the lifetime of if disposing is true;
  13. /// </summary>
  14. /// <param name="disposing">True if managed objects should be disposed, if false, only unmanaged resources should be released.</param>
  15. protected abstract void Dispose(bool disposing);
  16. /// <summary>
  17. /// Throws and <see cref="ObjectDisposedException"/> if the <see cref="IsDisposed"/> property is true.
  18. /// </summary>
  19. /// <seealso cref="IsDisposed"/>
  20. /// <exception cref="ObjectDisposedException">Thrown if the <see cref="IsDisposed"/> property is true.</exception>
  21. /// <seealso cref="Dispose()"/>
  22. protected virtual void ThrowIfDisposed()
  23. {
  24. if (this.IsDisposed)
  25. {
  26. throw new ObjectDisposedException(this.GetType().FullName);
  27. }
  28. }
  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. public string BuildMessage(string header, Dictionary<string, string> values)
  39. {
  40. var builder = new StringBuilder();
  41. const string argFormat = "{0}: {1}\r\n";
  42. builder.AppendFormat("{0}\r\n", header);
  43. foreach (var pair in values)
  44. {
  45. builder.AppendFormat(argFormat, pair.Key, pair.Value);
  46. }
  47. builder.Append("\r\n");
  48. return builder.ToString();
  49. }
  50. /// <summary>
  51. /// Disposes this object instance and all internally managed resources.
  52. /// </summary>
  53. /// <remarks>
  54. /// <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>
  55. /// </remarks>
  56. /// <seealso cref="IsDisposed"/>
  57. [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.")]
  58. public void Dispose()
  59. {
  60. IsDisposed = true;
  61. Dispose(true);
  62. }
  63. }
  64. }