示例#1
0
        private static T IntPtrToCallback <T>(IntPtr handle, bool unpinHandle) where T : class
        {
            if (PInvokeUtil.IsNull(handle))
            {
                return(null);
            }

            var gcHandle = GCHandle.FromIntPtr(handle);

            try
            {
                return((T)gcHandle.Target);
            }
            catch (System.InvalidCastException e)
            {
                Debug.LogError("GC Handle pointed to unexpected type: " + gcHandle.Target.ToString() +
                               ". Expected " + typeof(T));
                throw e;
            }
            finally
            {
                if (unpinHandle)
                {
                    gcHandle.Free();
                }
            }
        }
示例#2
0
 private void Cleanup()
 {
     if (!PInvokeUtil.IsNull(mSelfPointer))
     {
         ReleaseHandle(mSelfPointer);
         mSelfPointer = new HandleRef(this, IntPtr.Zero);
     }
 }
示例#3
0
        public virtual bool Equals(InteropObject other)
        {
            if (null == other)
            {
                return(false);
            }

            if (PInvokeUtil.IsNull(this.mSelfPointer))
            {
                return(PInvokeUtil.IsNull(other.mSelfPointer));
            }

            return(mSelfPointer.Handle == other.mSelfPointer.Handle && this.mIsDisposed == other.mIsDisposed);
        }
示例#4
0
        /// <summary>
        /// Creates a managed binding object for the unmanaged object referenced by the given pointer.
        /// If such binding object exists, this method returns it instead of creating a new one.
        /// Therefore using this method guarantees that the same managed binder is always returned
        /// for the same native pointer.
        /// </summary>
        /// <returns>The binder for the given pointer, or null if the pointer is invalid.</returns>
        /// <param name="pointer">Pointer.</param>
        /// <param name="constructor">Constructor.</param>
        public static T FromPointer(IntPtr pointer, Func <IntPtr, T> constructor)
        {
            if (PInvokeUtil.IsNull(pointer))
            {
                return(default(T));
            }

            // Check if an binder exists for this IntPtr and return it.
            T binder = FindExistingBinder(pointer);

            if (binder != default(T))
            {
                return(binder);
            }

            // Otherwise create a new binder, add it to the binders map and return it.
            T newBinder = constructor(pointer);

            RegisterNewBinder(pointer, newBinder);
            return(newBinder);
        }
示例#5
0
 protected bool IsDisposed()
 {
     return(PInvokeUtil.IsNull(mSelfPointer));
 }
示例#6
0
 public bool IsDisposed()
 {
     return(PInvokeUtil.IsNull(mSelfPointer));
 }