Example #1
0
        /// <summary>
        /// Gets or sets the item with the specified key.
        /// </summary>
        /// <value></value>
        public TValue this[TKey key]
        {
            get
            {
                Object tempKey = key;
                TValue rvalue;
                if (this.dictionary.TryGetValue(tempKey, out rvalue))
                {
                    WeakReference <TKey> weakKey = (WeakReference <TKey>)tempKey;
                    if (weakKey.IsAlive)
                    {
                        return(rvalue);
                    }

                    this.dictionary.Remove(key);
                }

                throw new KeyNotFoundException("Value '" + key + "' not found");
            }
            set
            {
                if (key == null)
                {
                    throw new ArgumentNullException("key");
                }
                WeakReference <TKey> weakKey = new WeakKeyReference <TKey>(key, this.comparer);
                this.dictionary[weakKey] = value;
            }
        }
Example #2
0
        /// <summary>
        /// Tries to get the value.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public bool TryGetValue(
            TKey key,
            out TValue value)
        {
            if (key == null)
            {
                value = default;
                return(false);
            }

            WeakReference <TKey> tempKey = new WeakKeyReference <TKey>(key, comparer);

            TValue rvalue;

            if (dictionary.TryGetValue(tempKey, out rvalue))
            {
                var weakKey = (WeakReference <TKey>)tempKey;
                if (weakKey.IsAlive)
                {
                    value = rvalue;
                    return(true);
                }

                dictionary.Remove(key);
            }

            value = default(TValue);
            return(false);
        }
Example #3
0
        /// <summary>
        /// Sets the value.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        private void SetValue(
            TKey key,
            TValue value)
        {
            WeakReference <TKey> weakKey = new WeakKeyReference <TKey>(key, comparer);

            dictionary[weakKey] = value;
        }
Example #4
0
        public int GetHashCode(object obj)
        {
            WeakKeyReference <T> weakKey = obj as WeakKeyReference <T>;

            if (weakKey != null)
            {
                return(weakKey.HashCode);
            }
            return(this.comparer.GetHashCode((T)obj));
        }
Example #5
0
        /// <summary>
        /// Adds an element with the provided key and value to the <see cref="T:System.Collections.Generic.IDictionary`2"></see>.
        /// </summary>
        /// <param name="key">The object to use as the key of the element to add.</param>
        /// <param name="value">The object to use as the value of the element to add.</param>
        /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IDictionary`2"></see> is read-only.</exception>
        /// <exception cref="T:System.ArgumentException">An element with the same key already exists in the <see cref="T:System.Collections.Generic.IDictionary`2"></see>.</exception>
        /// <exception cref="T:System.ArgumentNullException">key is null.</exception>
        public void Add(TKey key, TValue value)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            WeakReference <TKey> weakKey = new WeakKeyReference <TKey>(key, this.comparer);

            this.dictionary.Add(weakKey, value);
        }
Example #6
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);
        }