Advertisement
GeneralGDA

Disposable

Apr 17th, 2019
1,299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.35 KB | None | 0 0
  1. // The MIT License (MIT)
  2.  
  3. // Copyright (c) 2018 Ltd Cadwise-N
  4.  
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. // this software and associated documentation files (the "Software"), to deal in
  7. // the Software without restriction, including without limitation the rights to
  8. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. // the Software, and to permit persons to whom the Software is furnished to do so,
  10. // subject to the following conditions:
  11.  
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14.  
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21.  
  22. using System;
  23. using JetBrains.Annotations;
  24.  
  25. namespace Cadwise.Scaffolding
  26. {
  27.     [PublicAPI]
  28.     public abstract class Disposable : IDisposable
  29.     {
  30.         private bool _disposed;
  31.  
  32.         ~Disposable()
  33.         {
  34.             Dispose(false);
  35.         }
  36.  
  37.         protected void ThrowIfDisposed()
  38.         {
  39.             if (_disposed)
  40.             {
  41.                 throw new ObjectDisposedException(GetType().FullName);
  42.             }
  43.         }
  44.  
  45.         public void Dispose()
  46.         {
  47.             Dispose(true);
  48.             GC.SuppressFinalize(this);
  49.         }
  50.  
  51.         private void Dispose(bool disposing)
  52.         {
  53.             if (_disposed)
  54.             {
  55.                 return;
  56.             }
  57.  
  58.             if (disposing)
  59.             {
  60.                 FreeManagedResources();
  61.             }
  62.  
  63.             FreeUnmanagedResources();
  64.  
  65.             _disposed = true;
  66.         }
  67.  
  68.         // может вызываться в финализаторе
  69.         protected virtual void FreeUnmanagedResources()
  70.         {
  71.         }
  72.  
  73.         // гарантированно не будет вызываться в финализаторе
  74.         protected virtual void FreeManagedResources()
  75.         {
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement