CSharp examples for System.Collections.Generic:ICollection
Removes an item from the collection.
// (c) Copyright Microsoft Corporation. using System.Reflection; using System.Linq; using System.Diagnostics.CodeAnalysis; using System.ComponentModel; using System.Collections; using System;/* w w w . j a v a 2s. co m*/ public class Main{ /// <summary> /// Removes an item from the collection. /// </summary> /// <param name="collection">The collection.</param> /// <param name="item">The item to be removed.</param> public static void Remove(this IEnumerable collection, object item) { ICollectionView collectionView = collection as ICollectionView; if (collectionView != null) { collectionView.SourceCollection.Remove(item); } else { Type genericListType = collection.GetType().GetInterfaces().Where(interfaceType => interfaceType.FullName.StartsWith("System.Collections.Generic.IList`1", StringComparison.Ordinal)).FirstOrDefault(); if (genericListType != null) { int index = (int)genericListType.GetMethod("IndexOf").Invoke(collection, new object[] { item }); if (index != -1) { genericListType.GetMethod("RemoveAt").Invoke(collection, new object[] { index }); } } else { IList list = collection as IList; list.Remove(item); } } } }