コード例 #1
0
        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));
            }
        }
コード例 #2
0
 public static VisiblyGarbageCollectedObject Create(booleanWrapper flag)
 {
     return(new VisiblyGarbageCollectedObject(() => flag.Value = true));
 }