Index: src/Common/DelftTools.Utils/WeakDictionary.cs =================================================================== diff -u -r18f9b18cab9da0c768badb3084415e993a5414ee -r5fc71a385897af92ccb092f2f969b5709afab85a --- src/Common/DelftTools.Utils/WeakDictionary.cs (.../WeakDictionary.cs) (revision 18f9b18cab9da0c768badb3084415e993a5414ee) +++ src/Common/DelftTools.Utils/WeakDictionary.cs (.../WeakDictionary.cs) (revision 5fc71a385897af92ccb092f2f969b5709afab85a) @@ -21,16 +21,9 @@ where TKey : class where TValue : class { - // keeps our keys weak, we keep our values weak ourselves + // keeps our keys weak, we keep our values weak ourselves private readonly ConditionalWeakTable innerDictionary = new ConditionalWeakTable(); - public void Add(TKey key, TValue value) - { - if (ContainsKey(key)) - throw new ArgumentException("Key already exists: " + key); - innerDictionary.Add(key, new WeakReference(value)); - } - public TValue this[TKey key] { get @@ -40,7 +33,9 @@ { var entry = (TValue) weakRef.Target; if (entry != null) + { return entry; + } innerDictionary.Remove(key); // entry no longer alive: cleanup } return null; @@ -55,6 +50,15 @@ } } + public void Add(TKey key, TValue value) + { + if (ContainsKey(key)) + { + throw new ArgumentException("Key already exists: " + key); + } + innerDictionary.Add(key, new WeakReference(value)); + } + public void Remove(TKey key) { innerDictionary.Remove(key); @@ -66,7 +70,9 @@ if (innerDictionary.TryGetValue(key, out weakRef)) { if (weakRef.IsAlive) + { return true; + } innerDictionary.Remove(key); // entry no longer alive: cleanup } return false; @@ -83,4 +89,4 @@ return false; } } -} +} \ No newline at end of file