internal static GCHandleProxy Alloc(object value) { var gch = new GCHandleProxy(System.Runtime.InteropServices.GCHandle.Alloc(value)); var weak = new WeakReference(value); var oldWeak = allocatedHandles.GetOrAdd(ToIntPtr(gch), weak); if (weak != oldWeak) { throw new InvalidOperationException( $"GC handle has already been allocated for this memory location. " + $"Old: {oldWeak.Target} New: {value}"); } return(gch); }
internal static IntPtr ToIntPtr(GCHandleProxy value) => System.Runtime.InteropServices.GCHandle.ToIntPtr(value.gch);