What Was I Thinking?

Follies & Foils of .NET Development
posts - 95 , comments - 352 , trackbacks - 0

Thursday, April 28, 2011

QuickTip: Determine if Type implements IEnumerable<T>

I was working on a project that required a determination if the type was an IEnumerable collection of some type. 

 

Using a bit of reflection and the handy-dandy GetGenericTypeDefinition method, I arrive at this:

Code Snippet
  1. public static bool IsIEnumerableOfT(Type type)
  2.       {
  3.           return type.GetInterfaces().Any(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof (IEnumerable<>)) ;
  4.       }

Happy Coding!

Posted On Thursday, April 28, 2011 8:26 AM | Comments (10) | Filed Under [ Visual Studio ]

Powered by: