Beispiel #1
0
        private void PrepareForReuse(Entry entry)
        {
            foreach (var item in entry.Values)
            {
                entriesByValue.Remove(item.Value);
            }
            entry.Values.Clear();

            format.ClearIdentity(entry.Node);
        }
Beispiel #2
0
        /// <summary>
        /// Removes every binding where the object is either the target or the source.
        /// </summary>
        /// <param name="obj">The object.</param>
        public static void ClearAllBindings(this INotifyPropertyChanged obj)
        {
            var    pairable = obj as IPairable;
            object pair     = pairable == null ? null : pairable.Pair;

            foreach (var kvp in bindingSet)
            {
                var list = kvp.Value;
                if (list == null)
                {
                    continue;
                }

                if (kvp.Key == obj || kvp.Key == pair)
                {
                    foreach (var binding in list)
                    {
                        binding.Dispose();
                    }

                    list.Clear();
                }
                else
                {
                    for (int i = list.Count - 1; i >= 0; i--)
                    {
                        var binding = list[i];
                        if (binding.Source == obj || (binding.Source != null && binding.Source == pair))
                        {
                            binding.Dispose();
                            list.RemoveAt(i);
                        }
                    }
                }
            }

            bindingSet.Remove(obj);

            if (pair != null)
            {
                bindingSet.Remove(pair);
            }
        }
Beispiel #3
0
 public void WeakKeyDictionary_RemoveKeyValuePair_EntryNotRemoved()
 {
     using (var key = new DisposableTestObject("Just kidding!"))
     {
         const int FortyTwo   = 42;
         var       dictionary = new WeakKeyDictionary <DisposableTestObject, int>();
         Assert.True(dictionary.AddEntry(key, FortyTwo));
         Assert.False(dictionary.Remove(new KeyValuePair <DisposableTestObject, int>(key, 54)));
         Assert.True(dictionary.ContainsKey(key));
     }
 }
Beispiel #4
0
 public void WeakKeyDictionary_RemoveKey_EntryRemoved()
 {
     using (var key = new DisposableTestObject("That's it, I'm outta here!"))
     {
         const int FortyTwo   = 42;
         var       dictionary = new WeakKeyDictionary <DisposableTestObject, int>();
         Assert.True(dictionary.AddEntry(key, FortyTwo));
         Assert.True(dictionary.Remove(key));
         Assert.False(dictionary.ContainsKey(key));
     }
 }
Beispiel #5
0
 public void WeakKeyDictionary_RemoveKeyAsObject_EntryRemoved()
 {
     using (var key = new DisposableTestObject("Remove Me, too!"))
     {
         const int FortyTwo   = 42;
         var       dictionary = new WeakKeyDictionary <DisposableTestObject, int>();
         Assert.True(dictionary.AddEntry(key, FortyTwo));
         dictionary.Remove((object)key);
         Assert.False(dictionary.ContainsKey(key));
     }
 }
Beispiel #6
0
        private void PrepareForReuse(Entry entry)
        {
            foreach (var item in entry.Values)
            {
                var value = item.Value.Target;
                if (null != value)
                {
                    entriesByValue.Remove(value);
                }
            }
            entry.Values.Clear();

            format.ClearIdentity(entry.Node);
        }
Beispiel #7
0
        public void TestRemove()
        {
            WeakKeyDictionary<object, string> dictionary =
                new WeakKeyDictionary<object, string>();
            dictionary[new object()] = "test";
            dictionary[new object()] = "foo";
            dictionary[new object()] = "bar";
            Assert.AreEqual(3, dictionary.Count);

            object testObject = new object();
            dictionary[testObject] = "blah";
            Assert.AreEqual(4, dictionary.Count);
            Assert.AreEqual(4, dictionary.Count);
            Assert.IsTrue(dictionary.ContainsKey(testObject));
            Assert.IsTrue(dictionary.Remove(testObject));
            Assert.AreEqual(3, dictionary.Count);

            GC.Collect();
            GC.WaitForPendingFinalizers();
            foreach (object key in dictionary.Keys)
            {
                Console.WriteLine("gen[{0}] = {1}", key, GC.GetGeneration(key));
            }
            Assert.AreEqual(0, dictionary.Count);
            Assert.AreEqual(0, dictionary.Values.Count);
        }
        public void Test_WeakKeyDictionary_CRUD()
        {
            var weakKeyDictionary = new WeakKeyDictionary<TestKey, int>();

            var key = new TestKey("Yohan");

            weakKeyDictionary.Add(key, 1);
            Assert.AreEqual(1, weakKeyDictionary.Count);
            Assert.AreEqual(1, weakKeyDictionary[key]);

            weakKeyDictionary.Remove(key);
            Assert.AreEqual(0, weakKeyDictionary.Count);

            Exception exception = null;
            try
            {
                weakKeyDictionary.Remove(null);
            }
            catch (ArgumentNullException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            exception = null;
            try
            {
                var i = weakKeyDictionary[new TestKey("1")];
            }
            catch (KeyNotFoundException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            key = new TestKey("Yohan2");
            weakKeyDictionary.Add(key, 1);
            Assert.AreEqual(1, weakKeyDictionary[key]);

            weakKeyDictionary.Clear();
            Assert.AreEqual(0, weakKeyDictionary.Count);

            weakKeyDictionary.Add(key, 1);
            Assert.IsTrue(weakKeyDictionary.ContainsKey(key));
            Assert.IsTrue(weakKeyDictionary.ContainsValue(1));

            weakKeyDictionary[key] = 2;
            Assert.AreEqual(2, weakKeyDictionary[key]);

            bool contains = weakKeyDictionary.ContainsValue(2);
            Assert.IsTrue(contains);

            exception = null;
            try
            {
                weakKeyDictionary[null] = 3;
            }
            catch (ArgumentNullException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            exception = null;
            try
            {
                weakKeyDictionary.Add(key, 1);
            }
            catch (ArgumentException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            int value;
            weakKeyDictionary.TryGetValue(key, out value);
            Assert.AreEqual(2, value);

            var count = weakKeyDictionary.Count;
            key = null;
            GC.Collect();

            weakKeyDictionary.Add(new TestKey("yohan9"), 2);

            Assert.AreEqual(count, weakKeyDictionary.Keys.Count);
        }
        public void Test_WeakKeyDictionary_CRUD()
        {
            var weakKeyDictionary = new WeakKeyDictionary <TestKey, int>();

            var key = new TestKey("Yohan");

            weakKeyDictionary.Add(key, 1);
            Assert.AreEqual(1, weakKeyDictionary.Count);
            Assert.AreEqual(1, weakKeyDictionary[key]);

            weakKeyDictionary.Remove(key);
            Assert.AreEqual(0, weakKeyDictionary.Count);

            Exception exception = null;

            try
            {
                weakKeyDictionary.Remove(null);
            }
            catch (ArgumentNullException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            exception = null;
            try
            {
                var i = weakKeyDictionary[new TestKey("1")];
            }
            catch (KeyNotFoundException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            key = new TestKey("Yohan2");
            weakKeyDictionary.Add(key, 1);
            Assert.AreEqual(1, weakKeyDictionary[key]);

            weakKeyDictionary.Clear();
            Assert.AreEqual(0, weakKeyDictionary.Count);

            weakKeyDictionary.Add(key, 1);
            Assert.IsTrue(weakKeyDictionary.ContainsKey(key));
            Assert.IsTrue(weakKeyDictionary.ContainsValue(1));

            weakKeyDictionary[key] = 2;
            Assert.AreEqual(2, weakKeyDictionary[key]);

            bool contains = weakKeyDictionary.ContainsValue(2);

            Assert.IsTrue(contains);

            exception = null;
            try
            {
                weakKeyDictionary[null] = 3;
            }
            catch (ArgumentNullException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            exception = null;
            try
            {
                weakKeyDictionary.Add(key, 1);
            }
            catch (ArgumentException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            int value;

            weakKeyDictionary.TryGetValue(key, out value);
            Assert.AreEqual(2, value);

            var count = weakKeyDictionary.Count;

            key = null;
            GC.Collect();

            weakKeyDictionary.Add(new TestKey("yohan9"), 2);

            Assert.AreEqual(count, weakKeyDictionary.Keys.Count);
        }