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