public object GetOrAdd(object key, object value) { var keyref = new WeakKeyRef(key, refq); writeLock.@lock(); try { DiscardWriteLocked(); object key2; var keyref2 = (WeakKeyRef)map.get(keyref); if (keyref2 != null && (key2 = keyref2.get()) != null) { value = keyref2.value; } else if (value != null) { keyref.value = value; map.put(keyref, keyref); } } finally { writeLock.unlock(); } return(value); }
public void Add(TKey key, TValue value) { var weakKey = new WeakKeyRef <TKey>(key, this.comparer); var weakValue = new SafeWeakRef <TValue>(value); dictionary.Add(weakKey, weakValue); }
public int GetHashCode(object obj) { WeakKeyRef <T> weakKey = obj as WeakKeyRef <T>; if (weakKey != null) { return(weakKey.HashCode); } return(this.comparer.GetHashCode((T)obj)); }
private static T GetTarget(object obj, out bool isDead) { WeakKeyRef <T> wref = obj as WeakKeyRef <T>; T target; if (wref != null) { target = wref.Target; isDead = !wref.IsAlive; } else { target = (T)obj; isDead = false; } return(target); }
public TValue this[TKey key] { get { TValue result; if (TryGetValue(key, out result)) { return(result); } else { throw new KeyNotFoundException(); } } set { var weakKey = new WeakKeyRef <TKey>(key, comparer); this.dictionary[weakKey] = new SafeWeakRef <TValue>(value); } }