Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyOtherClassBase
- {
- // ...
- }
- public class MyDerivedClass : MyOtherClassBase, ICalibrable
- {
- private bool m_bDisposed = false;
- ~MyDerivedClass()
- {
- Dispose(false);
- }
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- protected virtual void Dispose(bool bDisposing)
- {
- if (!m_bDisposed) {
- if (bDisposing) {
- // Dispose managed resources
- }
- // Dispose unmanaged resources
- }
- m_bDisposed = true;
- Type baseType = typeof(MyDerivedClass).BaseType;
- if (baseType != null) {
- if (baseType.GetInterface("IDisposable") != null) {
- // If you have no ancestors implementing base.Dispose(...), comment
- // the following line AND uncomment the throw.
- //
- // This way, if any of your ancestors decide one day to implement
- // IDisposable you will know about it right away and proceed to
- // uncomment the base.Dispose(...) in addition to commenting the throw.
- //base.Dispose(bDisposing);
- throw new ApplicationException("Ancestor base.Dispose(...) not called - "
- + baseType.ToString());
- }
- }
- }
- }
- public class MyVeryBaseClass
- {
- protected void RealDispose(bool isDisposing)
- {
- IDisposable tryme = this as IDisposable;
- if (tryme != null) // we implement IDisposable
- {
- this.Dispose();
- base.RealDispose(isDisposing);
- }
- }
- }
- public class FirstChild : MyVeryBaseClasee
- {
- //non-disposable
- }
- public class SecondChild : FirstChild, IDisposable
- {
- ~SecondChild()
- {
- Dispose(false);
- }
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- base.RealDispose(true);
- }
- protected virtual void Dispose(bool bDisposing)
- {
- if (!m_bDisposed) {
- if (bDisposing) {
- // Dispose managed resources
- }
- // Dispose unmanaged resources
- }
- }
- // Disposal Helper Functions
- public static class Disposing
- {
- // Executes IDisposable.Dispose() if it exists.
- public static void DisposeSuperclass(object o)
- {
- Type baseType = o.GetType().BaseType;
- bool superclassIsDisposable = typeof(IDisposable).IsAssignableFrom(baseType);
- if (superclassIsDisposable)
- {
- System.Reflection.MethodInfo baseDispose = baseType.GetMethod("Dispose", new Type[] { });
- baseDispose.Invoke(o, null);
- }
- }
- }
- class classA: IDisposable
- {
- public void Dispose()
- {
- Console.WriteLine("Disposing A");
- }
- }
- class classB : classA, IDisposable
- {
- }
- class classC : classB, IDisposable
- {
- public void Dispose()
- {
- Console.WriteLine("Disposing C");
- Disposing.DisposeSuperclass(this);
- }
- }
- class classA: IDisposable
- {
- private bool m_bDisposed;
- protected virtual void Dispose(bool bDisposing)
- {
- if (!m_bDisposed)
- {
- if (bDisposing)
- {
- // Dispose managed resources
- Console.WriteLine("Dispose A");
- }
- // Dispose unmanaged resources
- }
- }
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- Console.WriteLine("Disposing A");
- }
- }
- class classB : classA, IDisposable
- {
- private bool m_bDisposed;
- public void Dispose()
- {
- Dispose(true);
- base.Dispose();
- GC.SuppressFinalize(this);
- Console.WriteLine("Disposing B");
- }
- protected override void Dispose(bool bDisposing)
- {
- if (!m_bDisposed)
- {
- if (bDisposing)
- {
- // Dispose managed resources
- Console.WriteLine("Dispose B");
- }
- // Dispose unmanaged resources
- }
- }
- }
- class classC : classB, IDisposable
- {
- private bool m_bDisposed;
- public void Dispose()
- {
- Dispose(true);
- base.Dispose();
- GC.SuppressFinalize(this);
- Console.WriteLine("Disposing C");
- }
- protected override void Dispose(bool bDisposing)
- {
- if (!m_bDisposed)
- {
- if (bDisposing)
- {
- // Dispose managed resources
- Console.WriteLine("Dispose C");
- }
- // Dispose unmanaged resources
- }
- }
- }
Add Comment
Please, Sign In to add comment