public bool? TryCreateRef(Type interfaceType, out IObjectRef newObjectRef) { RefTrackedObject cleanupContainer; Validate.IsNotNull<Type>(interfaceType, "interfaceType"); IObjectRef innerRef = this.innerRef; if (innerRef == null) { newObjectRef = null; return null; } bool? nullable = innerRef.TryCreateRef(interfaceType, out newObjectRef); if (!nullable.GetValueOrDefault()) { return null; } ObjectRefProxy proxy = this; lock (proxy) { this.EnsureCleanupContainerCreatedWhileLocked(); cleanupContainer = this.cleanupContainer; } ICleanupContainer container = newObjectRef as ICleanupContainer; if (container == null) { ObjectRefProxy proxy2 = Create(interfaceType, newObjectRef, ObjectRefProxyOptions.AssumeOwnership); proxy2.AddCleanupRef(cleanupContainer); newObjectRef = proxy2; return nullable; } container.AddCleanupRef(cleanupContainer); return nullable; }
private void DisposeCore(bool disposing) { if (Interlocked.Exchange(ref this.isDisposed, 1) == 0) { try { this.Dispose(disposing); } finally { IObjectRef innerRef = this.innerRef; this.InnerRef = null; if ((this.proxyOptions & ObjectRefProxyOptions.DoNotCreateRef) != ObjectRefProxyOptions.DoNotCreateRef) { IRefTrackedObject obj2 = innerRef as IRefTrackedObject; if (obj2 != null) { obj2.ReleaseRef(this, disposing); } } } if (disposing) { RefTrackedObject cleanupContainer; ObjectRefProxy proxy = this; lock (proxy) { cleanupContainer = this.cleanupContainer; this.cleanupContainer = null; } DisposableUtil.Free<RefTrackedObject>(ref cleanupContainer, disposing); } } }
private void EnsureCleanupContainerCreatedWhileLocked() { if (this.cleanupContainer == null) { this.cleanupContainer = new RefTrackedObject(); } }
public static UnsafeObjectRef?TryCreate(RefTrackedObject objectRef) { Validate.IsNotNull <RefTrackedObject>(objectRef, "objectRef"); if (objectRef.TryAddUntrackedRef()) { return(new UnsafeObjectRef(objectRef)); } return(null); }
public static UnsafeObjectRef Create(RefTrackedObject objectRef) { Validate.IsNotNull <RefTrackedObject>(objectRef, "objectRef"); UnsafeObjectRef?nullable = TryCreate(objectRef); if (!nullable.HasValue) { ExceptionUtil.ThrowObjectDisposedException(objectRef.GetType().FullName); } return(nullable.Value); }
public void Dispose() { if (this.objectRef != null) { RefTrackedObject objectRef = this.objectRef as RefTrackedObject; if (objectRef != null) { objectRef.ReleaseUntrackedRef(true); } else { this.objectRef.Dispose(); } this.objectRef = null; } }
public static UnsafeObjectRef?TryCreate(IObjectRef objectRef) { IObjectRef ref2; Validate.IsNotNull <IObjectRef>(objectRef, "objectRef"); RefTrackedObject obj2 = objectRef as RefTrackedObject; if (obj2 != null) { return(TryCreate(obj2)); } if (objectRef.TryCreateRef(typeof(IObjectRef), out ref2).GetValueOrDefault()) { return(new UnsafeObjectRef(ref2)); } return(null); }
public static UnsafeObjectRef UseUnsafeRef(this RefTrackedObject objectRef) => UnsafeObjectRef.Create(objectRef);