A useful little class that you might find you need someday. It converts a weakly-typed list (like an IList of items) into a strongly typed one based on the type you feed it.
It's super simple to use. For example let's say you get back an IList<People> from some service (a data service, web service, etc.) but really need it to be a List<People>. You can just use this class to convert it for you.
I know, silly little example but just something for your code snippets that you can squirrel away for a rainy day.
1 public class ListToGenericListConverter<T>
3 /// <summary>
4 /// Converts a non-typed collection into a strongly typed collection. This will fail if
5 /// the non-typed collection contains anything that cannot be casted to type of T.
6 /// </summary>
7 /// <param name="listOfObjects">A <see cref="ICollection"/> of objects that will
8 /// be converted to a strongly typed collection.</param>
9 /// <returns>Always returns a valid collection - never returns null.</returns>
10 public List<T> ConvertToGenericList(IList listOfObjects)
12 ArrayList notStronglyTypedList = new ArrayList(listOfObjects);
13 return new List<T>(notStronglyTypedList.ToArray(typeof(T)) as T);