Exemple #1
0
 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;
 }
Exemple #2
0
 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);
         }
     }
 }
Exemple #3
0
 private void EnsureCleanupContainerCreatedWhileLocked()
 {
     if (this.cleanupContainer == null)
     {
         this.cleanupContainer = new RefTrackedObject();
     }
 }
Exemple #4
0
 public static UnsafeObjectRef?TryCreate(RefTrackedObject objectRef)
 {
     Validate.IsNotNull <RefTrackedObject>(objectRef, "objectRef");
     if (objectRef.TryAddUntrackedRef())
     {
         return(new UnsafeObjectRef(objectRef));
     }
     return(null);
 }
Exemple #5
0
        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);
        }
Exemple #6
0
 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;
     }
 }
Exemple #7
0
        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);
        }
Exemple #8
0
 public static UnsafeObjectRef UseUnsafeRef(this RefTrackedObject objectRef) =>
 UnsafeObjectRef.Create(objectRef);