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(); } } }
private void Cleanup() { if (!PInvokeUtil.IsNull(mSelfPointer)) { ReleaseHandle(mSelfPointer); mSelfPointer = new HandleRef(this, IntPtr.Zero); } }
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); }
/// <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); }
protected bool IsDisposed() { return(PInvokeUtil.IsNull(mSelfPointer)); }
public bool IsDisposed() { return(PInvokeUtil.IsNull(mSelfPointer)); }