Generic List Converter Snippet

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>

    2 {

    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)

   11     {

   12         ArrayList notStronglyTypedList = new ArrayList(listOfObjects);

   13         return new List<T>(notStronglyTypedList.ToArray(typeof(T)) as T[]);

   14     }

   15 }

Published Tuesday, May 8, 2007 3:29 PM by Bil Simser
Filed under:

Comments

# re: Generic List Converter Snippet

Tuesday, May 8, 2007 6:54 PM by Wilco Bauwer

FWIW, I think this will result in copying the elements three times. ;) From listOfObjects to notStronglyTypedList, to a temporary array to the typed list that gets returned. Alternative version:

List<T> Conv...(IList listOfObjects) {

 List<T> items = new List<T>();

 foreach (T obj in listOfObjects) {

   items.Add(obj);

 }

 return items;

}

# re: Generic List Converter Snippet

Wednesday, May 9, 2007 4:57 AM by Muntedhar Alhakim

Can always use AddRange()...

# re: Generic List Converter Snippet

Wednesday, May 9, 2007 7:29 AM by commenter

I take it by IList<People> you mean "an IList containing People objects".

# re: Generic List Converter Snippet

Wednesday, May 9, 2007 8:53 AM by Casey Barton

A major benefit of Wilco's approach is that you can accept an IEnumerable instead of an IList, which makes it much more generally useful.

# re: Generic List Converter Snippet

Wednesday, May 9, 2007 2:48 PM by cpon

I also have an alternative which will twice.

List<T> Conv...(IList listOfObjects) {

T[] temp = new T[ listOfObjects.Count ];

listOfObjects.CopyTo( temp, 0 );

return new List<T>( temp );

}

# re: Generic List Converter Snippet

Thursday, May 10, 2007 7:14 AM by Bil Simser

@Scott: Yes, looking at it now I see it is. It was something in our codebase around NHibernate which I thought was useful but yes, it came from that article. Although its interesting now to see the alternatives. Might be interesting to compare and see what's the most efficient.

# re: Generic List Converter Snippet

Thursday, May 10, 2007 4:18 PM by Bil Simser

Let the bearded great white wizard speak, if anything it's Billy McCafferty that's the repulsive slug.

# re: Generic List Converter Snippet

Thursday, December 1, 2011 7:09 AM by Shal

Hi,

I have a generic list like HashSet<BL.BusinessEntity> in my business layer, and I have the same HashSet<DAL.BusinessEntity> in my data access layer, when 'am assigning the HashSet<BL.BusinessEntity> obj with HashSet<DAL.BusinessEntity> this obj, it is throwing the exception as mismatch. Is there any solution for this to type cast or translate the objects.

Thanks in advance