using System; using System.Runtime.InteropServices; namespace UTJ.FbxExporter { public class PinnedObject : IDisposable { public PinnedObject(T data) { this.m_data = data; this.m_gch = GCHandle.Alloc(this.m_data, GCHandleType.Pinned); } public T Object { get { return this.m_data; } } public IntPtr Pointer { get { return this.m_gch.AddrOfPinnedObject(); } } public void Dispose() { this.Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (disposing && this.m_gch.IsAllocated) { this.m_gch.Free(); } } public static implicit operator IntPtr(PinnedObject v) { return v.Pointer; } public static implicit operator T(PinnedObject v) { return v.Object; } private T m_data; private GCHandle m_gch; } }