Disposable.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.ComponentModel;
  3. namespace Microsoft
  4. {
  5. /// <summary>
  6. /// Dispose 模式
  7. /// </summary>
  8. public abstract class Disposable : IDisposable, IDisposed, IDisposeState
  9. {
  10. #region 静态字段
  11. /// <summary>
  12. /// 释放资源事件索引标记
  13. /// </summary>
  14. protected static readonly object EVENT_DISPOSED = new object();
  15. #endregion
  16. #region 字段属性
  17. private bool m_Disposing;
  18. /// <summary>
  19. /// 是否正在释放资源
  20. /// </summary>
  21. [Browsable(false)]
  22. public bool Disposing
  23. {
  24. get
  25. {
  26. return this.m_Disposing;
  27. }
  28. }
  29. private bool m_IsDisposed;
  30. /// <summary>
  31. /// 是否已经释放资源
  32. /// </summary>
  33. [Browsable(false)]
  34. public bool IsDisposed
  35. {
  36. get
  37. {
  38. return this.m_IsDisposed;
  39. }
  40. }
  41. private EventHandlerList m_Events;
  42. /// <summary>
  43. /// 事件列表
  44. /// </summary>
  45. [Browsable(false)]
  46. protected EventHandlerList Events
  47. {
  48. get
  49. {
  50. if (this.m_Events == null)
  51. this.m_Events = new EventHandlerList();
  52. return this.m_Events;
  53. }
  54. }
  55. #endregion
  56. #region 事件入口
  57. /// <summary>
  58. /// 释放资源事件
  59. /// </summary>
  60. public event EventHandler Disposed
  61. {
  62. add { this.Events.AddHandler(EVENT_DISPOSED, value); }
  63. remove { this.Events.RemoveHandler(EVENT_DISPOSED, value); }
  64. }
  65. #endregion
  66. #region 构造函数
  67. /// <summary>
  68. /// 构造函数
  69. /// </summary>
  70. public Disposable()
  71. {
  72. }
  73. /// <summary>
  74. /// 析构函数
  75. /// </summary>
  76. ~Disposable()
  77. {
  78. this.DisposeCore(false);
  79. }
  80. #endregion
  81. #region 私有方法
  82. /// <summary>
  83. /// 释放资源
  84. /// </summary>
  85. /// <param name="disposing">释放托管资源为true,否则为false</param>
  86. private void DisposeCore(bool disposing)
  87. {
  88. //调用限制
  89. if (this.m_Disposing)
  90. return;
  91. this.m_Disposing = true;
  92. //供子类重写
  93. this.Dispose(disposing);
  94. //释放事件列表
  95. if (this.m_Events != null)
  96. {
  97. EventHandler handler = (EventHandler)this.m_Events[EVENT_DISPOSED];
  98. if (handler != null)
  99. handler(this, EventArgs.Empty);
  100. this.m_Events.Dispose();
  101. this.m_Events = null;
  102. }
  103. //调用结束
  104. this.m_Disposing = false;
  105. this.m_IsDisposed = true;
  106. }
  107. #endregion
  108. #region 保护方法
  109. /// <summary>
  110. /// 释放资源
  111. /// </summary>
  112. /// <param name="disposing">释放托管资源为true,否则为false</param>
  113. protected abstract void Dispose(bool disposing);
  114. #endregion
  115. #region 公共方法
  116. /// <summary>
  117. /// 检查是否已释放资源,如果已释放资源则抛出异常
  118. /// </summary>
  119. public void CheckDisposed()
  120. {
  121. if (this.IsDisposed)
  122. throw new ObjectDisposedException(base.GetType().FullName);
  123. }
  124. /// <summary>
  125. /// 释放资源
  126. /// </summary>
  127. public void Dispose()
  128. {
  129. this.DisposeCore(true);
  130. GC.SuppressFinalize(this);
  131. }
  132. #endregion
  133. }
  134. }