private TResult handleMutate <TResult>( MultiDictionary <CompositionHost, object> exportsList, Func <List <object>, TResult> mutate, CompositionHost addUnder) { IEqualityComparer <object> referenceEqualityComparer = EquatableHelper.ReferenceEqualityComparer <object>(); MultiDictionary <CompositionHost, object> currentCopy = exports.CopyCollection(exportsList); List <object> currentValuesCopy = exportsList.GetAllValues(); List <object> currentValues = exportsList.GetAllValues(); TResult result = mutate(currentValues); // Removed? foreach (object currentValueCopy in currentValuesCopy) { if (currentValues.Contains(currentValueCopy, referenceEqualityComparer)) { continue; } KeyValuePair <CompositionHost, object> entry = currentCopy.FirstOrDefault(kv => object.ReferenceEquals(currentValueCopy, kv.Value)); exportsList.Remove(entry); if (!exportsList.ContainsKey(entry.Key) && !object.ReferenceEquals(MefSingletonComposer.unmanagedCompositionHost, entry.Key)) { entry.Key.Dispose(); } } // Added? foreach (object currentValue in currentValues) { if (currentValuesCopy.Contains(currentValue, referenceEqualityComparer)) { continue; } exportsList.AddValue(addUnder, currentValue); } return(result); }