Mike Nichols - Son of Nun Technology

Checking Generic Collection Types

Sometimes I get flustered with Generics. I love 'em, but working with their types are sometimes strange to me. One little thing I wanted was to check if a Type was a generic List<T> or not.

For example, if 'thing' might be a generic list and I want to check for that, this will fail:

            Type type = thing.GetType();

            type.Equals(typeof (IList<>)); //fails

Jon Skeet gave an answer in an ASP.NET forum that was the best way to check for this:

            Type type = thing.GetType();

            if (typeof(IEnumerable).IsAssignableFrom(t))

            {

               //Do Something

            }

The obvious failure here is that not every IEnumerable is an List<T>. But this is the best I can do at the moment...