DisposableManagedObjectBase.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Text;
  5. namespace Rssdp.Infrastructure
  6. {
  7. /// <summary>
  8. /// Correctly implements the <see cref="IDisposable"/> interface and pattern for an object containing only managed resources, and adds a few common niceties not on the interface such as an <see cref="IsDisposed"/> property.
  9. /// </summary>
  10. public abstract class DisposableManagedObjectBase : IDisposable
  11. {
  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)
  26. {
  27. throw new ObjectDisposedException(this.GetType().FullName);
  28. }
  29. }
  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. 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(CultureInfo.InvariantCulture, "{0}\r\n", header);
  44. foreach (var pair in values)
  45. {
  46. builder.AppendFormat(CultureInfo.InvariantCulture, ArgFormat, pair.Key, pair.Value);
  47. }
  48. builder.Append("\r\n");
  49. return builder.ToString();
  50. }
  51. /// <summary>
  52. /// Disposes this object instance and all internally managed resources.
  53. /// </summary>
  54. /// <remarks>
  55. /// <para>Sets the <see cref="IsDisposed"/> property to true. Does not explicitly throw an exception if called multiple times, but makes no promises about behavior of derived classes.</para>
  56. /// </remarks>
  57. /// <seealso cref="IsDisposed"/>
  58. [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 interfere with the dispose process.")]
  59. public void Dispose()
  60. {
  61. IsDisposed = true;
  62. Dispose(true);
  63. }
  64. }
  65. }