SmartGCHandle.cs 828 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Linq;
  5. namespace Helper
  6. {
  7. public class SmartGCHandle : IDisposable
  8. {
  9. private GCHandle handle;
  10. public SmartGCHandle(GCHandle handle)
  11. {
  12. this.handle = handle;
  13. }
  14. ~SmartGCHandle()
  15. {
  16. Dispose(false);
  17. }
  18. public System.IntPtr AddrOfPinnedObject()
  19. {
  20. return handle.AddrOfPinnedObject();
  21. }
  22. public virtual void Dispose()
  23. {
  24. Dispose(true);
  25. }
  26. protected virtual void Dispose(bool disposing)
  27. {
  28. this.handle.Free();
  29. }
  30. public static implicit operator GCHandle(SmartGCHandle other)
  31. {
  32. return other.handle;
  33. }
  34. }
  35. }