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
        /// <summary>
        /// Gets an object that represents the actual (serializable) view model of the specified object.
        /// </summary>
        public object GetOrCreateSubstitute(object obj)
        {
            Contract.Requires(obj != null, "Only non-null properties can be substituted");
            // Contract.Requires(obj.GetType().IsClass, "Only reference types can be substituted");

            if (!mappedObjects.TryGetValue(obj, out object substitute))
            {
                substitute = this.createSubstitute(obj);
                mappedObjects.Add(obj, substitute);
            }
            return(substitute);
        }