public void WeakReferenceCollection_Remove_Test1() { using (var verify = new Verify()) { string item1 = "AAA"; string item2 = "BBB"; var list = new List <string>(); list.Add(item1); list.Add(item2); var result = new WeakReferenceCollection <string>(); result.AddRange(list); verify.ItemsAreEqual(new[] { "AAA", "BBB" }, result, "Two items should have been in the collection."); verify.IsTrue(result.Remove(item2), "item 1 should have been removable"); verify.ItemsAreEqual(new[] { "AAA" }, result, "One item should have been left in the collection."); verify.IsTrue(result.Remove(item1), "item 2 should have been removable"); verify.IsEmpty(result, "Removal failed"); verify.IsFalse(result.Remove(item1), "item was already removed"); verify.ArgumentNullException("item", () => result.Remove(null)); } }
public void Invalidate(string cacheKey) { lock (SyncRoot) { foreach (var item in m_Cache) { if (item.Item1 == cacheKey) { m_Cache.Remove(item); return; } } } }
public void WeakReferenceCollection_Remove_Test2() { using (var verify = new Verify()) { var result = new WeakReferenceCollection <Foo>(); Action add = () => result.Add(new Foo()); add(); Memory.CycleGC(); var f = new Foo(); result.Add(f); result.Add(f); result.Remove(f); result.Remove(f); result.CleanUp(); verify.IsEmpty(result, ""); } }
public void Remove_ObjectShouldGetRemoved() { var obj = new object(); var wrc = new WeakReferenceCollection<object>(); wrc.Add(obj); Assert.AreEqual(1, wrc.AliveItemsToList().Count, "Should have 1 item!"); wrc.Remove(obj); Assert.AreEqual(0, wrc.AliveItemsToList().Count, "Should have 0 item!"); }
/// <summary> /// Remove a reference to the specified object (if there is a live reference). /// </summary> /// <param name="collection">The collection to remove the reference from.</param> /// <param name="id">The id of the object to remove references to.</param> public static void RemoveReferenceTo(this WeakReferenceCollection collection, DisposableObjectId id) { foreach (WeakReferenceOf <IManagedDisposableObject> weakreference in collection) { if (weakreference.Id.Equals(id)) { // We expect only one reference to the object collection.Remove(weakreference); Debug.Assert(collection.Where(x => x.Id.Equals(id)).Count() == 0, "Expected only one reference"); break; } } }
public void WeakReferenceCollection_Remove_Test2() { using (var verify = new Verify()) { var result = new WeakReferenceCollection <Foo>(); #pragma warning disable IDE0039 // Use local function Action add = () => result.Add(new Foo()); #pragma warning restore IDE0039 // Use local function add(); Memory.CycleGC(); var f = new Foo(); result.Add(f); result.Add(f); result.Remove(f); result.Remove(f); result.CleanUp(); verify.IsEmpty(result, ""); } }
/// <summary> /// Removes a weak event handler. /// </summary> /// <param name="eventHandler"></param> public void RemoveHandler(IListener <T> eventHandler) { if (eventHandler == null) { throw new ArgumentNullException(nameof(eventHandler), $"{nameof(eventHandler)} is null."); } m_Targets.Remove(eventHandler); m_Targets.CleanUp(); if (m_Listening && m_Targets.Count == 0) { m_Listening = false; DetachFromEvent(); } }
public void WeakReferenceCollection_Remove_Test2() { using (var verify = new Verify()) { var result = new WeakReferenceCollection<Foo>(); Action add = () => result.Add(new Foo()); add(); Memory.CycleGC(); var f = new Foo(); result.Add(f); result.Add(f); result.Remove(f); result.Remove(f); result.CleanUp(); verify.IsEmpty(result, ""); } }
public void WeakReferenceCollection_Remove_Test1() { using (var verify = new Verify()) { string item1 = "AAA"; string item2 = "BBB"; var list = new List<string>(); list.Add(item1); list.Add(item2); var result = new WeakReferenceCollection<string>(); result.AddRange(list); verify.ItemsAreEqual(new[] { "AAA", "BBB" }, result, "Two items should have been in the collection."); verify.IsTrue(result.Remove(item2), "item 1 should have been removable"); verify.ItemsAreEqual(new[] { "AAA" }, result, "One item should have been left in the collection."); verify.IsTrue(result.Remove(item1), "item 2 should have been removable"); verify.IsEmpty(result, "Removal failed"); verify.IsFalse(result.Remove(item1), "item was already removed"); verify.ArgumentNullException("item", () => result.Remove(null)); } }