Exemplo n.º 1
0
        public T GetDistinctInstance(T newInstance, out bool instanceAlreadyExists)
        {
            lock (monitor)
            {
                if (newInstance == null)
                {
                    throw new ArgumentNullException("newInstance");
                }

                HashableWeakReference weakRef = new HashableWeakReference(newInstance);
                if (this.distinctInstances.ContainsKey(weakRef))
                {
                    HashableWeakReference existing = this.distinctInstances[weakRef];
                    if (existing.IsAlive)
                    {
                        IDisposable disposable = newInstance as IDisposable;
                        if (disposable != null)
                        {
                            disposable.Dispose();
                        }

                        instanceAlreadyExists = true;
                        return((T)existing.Target);
                    }
                    else
                    {
                        this.distinctInstances.Remove(existing);
                    }
                }

                        #if !NO_AUTO_CLEAN_UP_OF_DISTINCT_INSTANCES
                if (this.AutoCleanup && this.autoCleanUpTimer == null)
                {
                    this.autoCleanUpTimer = new Timer(new TimerCallback((state) => this.CleanUpUnusedReferences()), null, 0, AUTO_CLEANUP_INTERVAL);
                }
                        #endif

                this.distinctInstances.Add(weakRef, weakRef);

                instanceAlreadyExists = false;
                return(newInstance);
            }
        }
Exemplo n.º 2
0
 public override bool Equals(object obj)
 {
     lock (this.monitor)
     {
         HashableWeakReference other = obj as HashableWeakReference;
         if (this.IsAlive)
         {
             if (other != null)
             {
                 return(other.IsAlive && object.Equals(this.Target, other.Target));
             }
             else
             {
                 return(object.Equals(this.Target, obj));
             }
         }
         else
         {
             return(false);
         }
     }
 }