예제 #1
0
        /// <summary>
        /// Adds an element with the provided key and value to the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
        /// </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.ArgumentNullException"><paramref name="key"/> is null.</exception><exception cref="T:System.ArgumentException">An element with the same key already exists in the <see cref="T:System.Collections.Generic.IDictionary`2"/>.</exception><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IDictionary`2"/> is read-only.</exception>
        public override void Add(TKey key, TValue value)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            var weakKey = new WeakKeyReference <TKey>(key, this.comparer);

            this.dictionary.Add(weakKey, value);
        }
예제 #2
0
        /// <summary>
        /// Returns a hash code for the specified object.
        /// </summary>
        /// <returns>
        /// A hash code for the specified object.
        /// </returns>
        /// <param name="obj">The <see cref="T:System.Object"/> for which a hash code is to be returned.</param><exception cref="T:System.ArgumentNullException">The type of <paramref name="obj"/> is a reference type and <paramref name="obj"/> is null.</exception>
        public int GetHashCode(object obj)
        {
            WeakKeyReference <T> weakKey = obj as WeakKeyReference <T>;

            if (weakKey != null)
            {
                return(weakKey.HashCode);
            }
            return(this.comparer.GetHashCode((T)obj));
        }
예제 #3
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);
        }
예제 #4
0
        /// <summary>
        /// Sets the value for the provided key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        protected override void SetValue(TKey key, TValue value)
        {
            var weakKey = new WeakKeyReference <TKey>(key, this.comparer);

            this.dictionary[weakKey] = value;
        }
예제 #5
0
        /// <summary>
        /// Sets the value for the provided key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        protected override void SetValue(TKey key, TValue value)
        {
            var weakKey = new WeakKeyReference <TKey>(key, this.comparer);

            this.dictionary[weakKey] = LogoFX.Core.WeakReference <TValue> .Create(value);
        }