public void Remove() { var valueCollected = new booleanWrapper(); var keyCollected = new booleanWrapper(); var dict = new WeakReferenceDictionary <VisiblyGarbageCollectedObject, VisiblyGarbageCollectedObject>(ReferenceEqualityComparer.Instance); addKeyAndValueWrappersToDict(); GC.Collect(); // collects the key dict.Clean(); // removes the last reference to the value waitForGC(); Assert.IsTrue(keyCollected); Assert.IsFalse(valueCollected); GC.Collect(); // collects the value waitForGC(); Assert.IsTrue(valueCollected); void addKeyAndValueWrappersToDict() { dict.Add(VisiblyGarbageCollectedObject.Create(keyCollected), VisiblyGarbageCollectedObject.Create(valueCollected)); } }
public static VisiblyGarbageCollectedObject Create(booleanWrapper flag) { return(new VisiblyGarbageCollectedObject(() => flag.Value = true)); }