code_junkie

Calling base.Dispose() automatically from derived classes

Nov 10th, 2011
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. public class MyOtherClassBase
  2. {
  3. // ...
  4. }
  5.  
  6.  
  7. public class MyDerivedClass : MyOtherClassBase, ICalibrable
  8. {
  9.  
  10. private bool m_bDisposed = false;
  11.  
  12. ~MyDerivedClass()
  13. {
  14. Dispose(false);
  15. }
  16.  
  17. public void Dispose()
  18. {
  19. Dispose(true);
  20. GC.SuppressFinalize(this);
  21. }
  22.  
  23. protected virtual void Dispose(bool bDisposing)
  24. {
  25. if (!m_bDisposed) {
  26. if (bDisposing) {
  27. // Dispose managed resources
  28. }
  29. // Dispose unmanaged resources
  30. }
  31. m_bDisposed = true;
  32.  
  33. Type baseType = typeof(MyDerivedClass).BaseType;
  34. if (baseType != null) {
  35. if (baseType.GetInterface("IDisposable") != null) {
  36. // If you have no ancestors implementing base.Dispose(...), comment
  37. // the following line AND uncomment the throw.
  38. //
  39. // This way, if any of your ancestors decide one day to implement
  40. // IDisposable you will know about it right away and proceed to
  41. // uncomment the base.Dispose(...) in addition to commenting the throw.
  42. //base.Dispose(bDisposing);
  43. throw new ApplicationException("Ancestor base.Dispose(...) not called - "
  44. + baseType.ToString());
  45. }
  46. }
  47. }
  48. }
  49.  
  50. public class MyVeryBaseClass
  51. {
  52. protected void RealDispose(bool isDisposing)
  53. {
  54. IDisposable tryme = this as IDisposable;
  55. if (tryme != null) // we implement IDisposable
  56. {
  57. this.Dispose();
  58. base.RealDispose(isDisposing);
  59. }
  60. }
  61. }
  62.  
  63. public class FirstChild : MyVeryBaseClasee
  64. {
  65. //non-disposable
  66. }
  67.  
  68. public class SecondChild : FirstChild, IDisposable
  69. {
  70. ~SecondChild()
  71. {
  72. Dispose(false);
  73. }
  74. public void Dispose()
  75. {
  76. Dispose(true);
  77. GC.SuppressFinalize(this);
  78. base.RealDispose(true);
  79. }
  80.  
  81. protected virtual void Dispose(bool bDisposing)
  82. {
  83. if (!m_bDisposed) {
  84. if (bDisposing) {
  85. // Dispose managed resources
  86. }
  87. // Dispose unmanaged resources
  88. }
  89. }
  90.  
  91. // Disposal Helper Functions
  92. public static class Disposing
  93. {
  94. // Executes IDisposable.Dispose() if it exists.
  95. public static void DisposeSuperclass(object o)
  96. {
  97. Type baseType = o.GetType().BaseType;
  98. bool superclassIsDisposable = typeof(IDisposable).IsAssignableFrom(baseType);
  99. if (superclassIsDisposable)
  100. {
  101. System.Reflection.MethodInfo baseDispose = baseType.GetMethod("Dispose", new Type[] { });
  102. baseDispose.Invoke(o, null);
  103. }
  104. }
  105. }
  106.  
  107. class classA: IDisposable
  108. {
  109. public void Dispose()
  110. {
  111. Console.WriteLine("Disposing A");
  112. }
  113. }
  114.  
  115. class classB : classA, IDisposable
  116. {
  117. }
  118.  
  119. class classC : classB, IDisposable
  120. {
  121. public void Dispose()
  122. {
  123. Console.WriteLine("Disposing C");
  124. Disposing.DisposeSuperclass(this);
  125. }
  126. }
  127.  
  128. class classA: IDisposable
  129. {
  130. private bool m_bDisposed;
  131. protected virtual void Dispose(bool bDisposing)
  132. {
  133. if (!m_bDisposed)
  134. {
  135. if (bDisposing)
  136. {
  137. // Dispose managed resources
  138. Console.WriteLine("Dispose A");
  139. }
  140. // Dispose unmanaged resources
  141. }
  142. }
  143. public void Dispose()
  144. {
  145. Dispose(true);
  146. GC.SuppressFinalize(this);
  147. Console.WriteLine("Disposing A");
  148. }
  149. }
  150.  
  151. class classB : classA, IDisposable
  152. {
  153. private bool m_bDisposed;
  154. public void Dispose()
  155. {
  156. Dispose(true);
  157. base.Dispose();
  158. GC.SuppressFinalize(this);
  159. Console.WriteLine("Disposing B");
  160. }
  161.  
  162. protected override void Dispose(bool bDisposing)
  163. {
  164. if (!m_bDisposed)
  165. {
  166. if (bDisposing)
  167. {
  168. // Dispose managed resources
  169. Console.WriteLine("Dispose B");
  170. }
  171. // Dispose unmanaged resources
  172. }
  173. }
  174. }
  175.  
  176. class classC : classB, IDisposable
  177. {
  178. private bool m_bDisposed;
  179. public void Dispose()
  180. {
  181. Dispose(true);
  182. base.Dispose();
  183. GC.SuppressFinalize(this);
  184. Console.WriteLine("Disposing C");
  185. }
  186. protected override void Dispose(bool bDisposing)
  187. {
  188. if (!m_bDisposed)
  189. {
  190. if (bDisposing)
  191. {
  192. // Dispose managed resources
  193. Console.WriteLine("Dispose C");
  194. }
  195. // Dispose unmanaged resources
  196. }
  197. }
  198. }
Add Comment
Please, Sign In to add comment