Ejemplo n.º 1
0
        /// <summary>
        /// Removes the left-over weak references for entries in the dictionary whose key has already been
        /// reclaimed by the garbage collector. This will reduce the dictionary's Count by the number of
        /// dead key-value pairs that were eliminated.
        /// </summary>
        public void RemoveCollectedEntries()
        {
            List <object> entriesToBeRemoved = new List <object>();

            foreach (var key in this.dictionary.Keys)
            {
                WeakKeyReference <TKey> weakKey = key as WeakKeyReference <TKey>;
                if (weakKey != null)
                {
                    if (!weakKey.IsAlive)
                    {
                        entriesToBeRemoved.Add(key);
                    }
                }
                else if (this.RemoveCollectedEntriesRules.Any(f => f(key)))
                {
                    entriesToBeRemoved.Add(key);
                }
            }

            foreach (var key in entriesToBeRemoved)
            {
                if (this.dictionary.Remove(key))
                {
                    this.countForRefresh--;
                }
            }

            // After a clean, we will let the dict to clean itself after adding the next intervalForRefresh(1000) items.
            this.countLimitForRefresh = this.countForRefresh + intervalForRefresh;
        }
        /// <summary>
        /// Get the hash code for the specified object.
        /// </summary>
        /// <param name="obj">The object of which a hash code is to be returned.</param>
        /// <returns>
        /// A hash code for the specified object.
        /// </returns>
        public override int GetHashCode(object obj)
        {
            WeakKeyReference <object> weakKey = obj as WeakKeyReference <object>;

            if (weakKey != null)
            {
                return(weakKey.HashCode);
            }

            Tuple <object, MemberInfo> tuple = obj as Tuple <object, MemberInfo>;

            if (tuple != null)
            {
                return(tuple.Item1.GetHashCode() ^ tuple.Item2.GetHashCode());
            }

            Tuple <WeakKeyReference <object>, MemberInfo> tuple2 = obj as Tuple <WeakKeyReference <object>, MemberInfo>;

            if (tuple2 != null)
            {
                return(tuple2.Item1.HashCode ^ tuple2.Item2.GetHashCode());
            }

            return(this.comparer.GetHashCode(obj));
        }
    public override void Add(TKey key, TValue value)
    {
        if (key == null)
        {
            throw new ArgumentNullException("key");
        }
        WeakReference <TKey> weakKey = new WeakKeyReference <TKey>(key, comparer);

        dictionary.Add(weakKey, value);
    }
Ejemplo n.º 4
0
    public int GetHashCode(object obj)
    {
        WeakKeyReference <T> weakKey = obj as WeakKeyReference <T>;

        if (weakKey != null)
        {
            return(weakKey.HashCode);
        }
        return(comparer.GetHashCode((T)obj));
    }
            public int GetHashCode(object obj)
            {
                WeakKeyReference weakKey = obj as WeakKeyReference;

                if (weakKey != null)
                {
                    return(weakKey.HashCode);
                }
                return(obj.GetHashCode());
            }
Ejemplo n.º 6
0
        public override void Add(TKey key, TValue value)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            WeakReference <TKey>   weakKey   = new WeakKeyReference <TKey>(key, this.comparer);
            WeakReference <TValue> weakValue = WeakReference <TValue> .Create(value);

            this.dictionary.Add(weakKey, weakValue);
        }
Ejemplo n.º 7
0
    private static T GetTarget(object obj, out bool isDead)
    {
        WeakKeyReference <T> wref = obj as WeakKeyReference <T>;
        T target;

        if (wref != null)
        {
            target = wref.Target;
            isDead = !wref.IsAlive;
        }
        else
        {
            target = (T)obj;
            isDead = false;
        }
        return(target);
    }
Ejemplo n.º 8
0
        /// <summary>
        /// Gets the target of the input object if it is a <see cref="WeakKeyReference&lt;T&gt;"/>, else return it self.
        /// </summary>
        /// <param name="obj">The input object from which to get the target.</param>
        /// <param name="isDead">Indicate whether the object is dead if it is a <see cref="WeakKeyReference&lt;T&gt;"/>.</param>
        /// <returns>The target of the input object.</returns>
        protected virtual T GetTarget(object obj, out bool isDead)
        {
            T target;

            WeakKeyReference <T> wref = obj as WeakKeyReference <T>;

            if (wref != null)
            {
                target = wref.Target;
                isDead = !wref.IsAlive;
            }
            else
            {
                target = (T)obj;
                isDead = false;
            }

            return(target);
        }
        /// <summary>
        /// Gets the target of the input object if it is a <see cref="WeakKeyReference&lt;T&gt;"/>,
        /// else a new Tuple&lt;object, MemberInfo&gt; if it is a Tuple&lt;WeakKeyReference&lt;object&gt;, MemberInfo&gt;.
        /// </summary>
        /// <param name="obj">The input object from which to get the target.</param>
        /// <param name="isDead">Indicate whether the object is dead if it is a <see cref="WeakKeyReference&lt;T&gt;"/>.
        /// Or whether the first item of a tuple is dead.
        /// </param>
        /// <returns>The target of the input object.</returns>
        protected override object GetTarget(object obj, out bool isDead)
        {
            WeakKeyReference <object> wref = obj as WeakKeyReference <object>;

            if (wref != null)
            {
                isDead = !wref.IsAlive;
                return(wref.Target);
            }

            Tuple <WeakKeyReference <object>, MemberInfo> tuple = obj as Tuple <WeakKeyReference <object>, MemberInfo>;

            if (tuple != null)
            {
                return(new Tuple <object, MemberInfo>(GetTarget(tuple.Item1, out isDead), tuple.Item2));
            }

            isDead = false;
            return(obj);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Adds a new item to the dictionary.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        public void Add(TKey key, TValue value)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            TValue local;

            if (TryGet(key, out local))
            {
                throw new ArgumentException("Resources.KeyAlreadyPresentInDictionary");
            }

            var weakKey = new WeakKeyReference(key);

            var weakValue = new WeakReference(EncodeNullObject(value));

            _inner.Add(weakKey, weakValue);
        }
Ejemplo n.º 11
0
    protected override void SetValue(TKey key, TValue value)
    {
        WeakReference <TKey> weakKey = new WeakKeyReference <TKey>(key, comparer);

        dictionary[weakKey] = value;
    }
Ejemplo n.º 12
0
        protected override void SetValue(TKey key, TValue value)
        {
            WeakReference <TKey> weakKey = new WeakKeyReference <TKey>(key, this.comparer);

            this.dictionary[weakKey] = WeakReference <TValue> .Create(value);
        }
Ejemplo n.º 13
0
    public int GetHashCode(object obj)
    {
        WeakKeyReference <T> weakKey = obj as WeakKeyReference <T>;

        return(weakKey?.HashCode ?? comparer.GetHashCode((T)obj));
    }