예제 #1
0
        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);
        }
예제 #2
0
 internal static IntPtr ToIntPtr(GCHandleProxy value) =>
 System.Runtime.InteropServices.GCHandle.ToIntPtr(value.gch);