/// <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; } } }