/// <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); }
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()); }
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); }
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); }
/// <summary> /// Gets the target of the input object if it is a <see cref="WeakKeyReference<T>"/>, 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<T>"/>.</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<T>"/>, /// else a new Tuple<object, MemberInfo> if it is a Tuple<WeakKeyReference<object>, MemberInfo>. /// </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<T>"/>. /// 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); }
/// <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); }
protected override void SetValue(TKey key, TValue value) { WeakReference <TKey> weakKey = new WeakKeyReference <TKey>(key, comparer); dictionary[weakKey] = value; }
protected override void SetValue(TKey key, TValue value) { WeakReference <TKey> weakKey = new WeakKeyReference <TKey>(key, this.comparer); this.dictionary[weakKey] = WeakReference <TValue> .Create(value); }
public int GetHashCode(object obj) { WeakKeyReference <T> weakKey = obj as WeakKeyReference <T>; return(weakKey?.HashCode ?? comparer.GetHashCode((T)obj)); }