Ejemplo n.º 1
0
        /// <summary>
        /// Removes the left-over weak references for entries in the dictionary
        /// whose key or value 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> toRemove = null;

            foreach (KeyValuePair <object, LogoFX.Core.WeakReference <TValue> > pair in this.dictionary)
            {
                var weakKey = (LogoFX.Core.WeakReference <TKey>)(pair.Key);
                LogoFX.Core.WeakReference <TValue> weakValue = pair.Value;

                if (!weakKey.IsAlive || !weakValue.IsAlive)
                {
                    if (toRemove == null)
                    {
                        toRemove = new List <object>();
                    }
                    toRemove.Add(weakKey);
                }
            }

            if (toRemove != null)
            {
                foreach (object key in toRemove)
                {
                    this.dictionary.Remove(key);
                }
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Returns an enumerator that iterates through the collection.
 /// </summary>
 /// <returns>
 /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
 /// </returns>
 /// <filterpriority>1</filterpriority>
 public override IEnumerator <KeyValuePair <TKey, TValue> > GetEnumerator()
 {
     foreach (KeyValuePair <object, LogoFX.Core.WeakReference <TValue> > kvp in this.dictionary)
     {
         var weakKey = (LogoFX.Core.WeakReference <TKey>)(kvp.Key);
         LogoFX.Core.WeakReference <TValue> weakValue = kvp.Value;
         TKey   key   = weakKey.Target;
         TValue value = weakValue.Target;
         if (weakKey.IsAlive && weakValue.IsAlive)
         {
             yield return(new KeyValuePair <TKey, TValue>(key, value));
         }
     }
 }