What Was I Thinking?

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

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!

Print | posted on Thursday, April 28, 2011 8:26 AM | Filed Under [ Visual Studio ]

Feedback

Gravatar

# re: QuickTip: Determine if Type implements IEnumerable<T>

A way to be a bit more efficient (by returning true as soon as possible):

return type.GetInterfaces().Any(t => t.GetGenericTypeDefinition() == typeof(IEnumerable<>));

:)
4/28/2011 11:06 AM | Gart
Gravatar

# re: QuickTip: Determine if Type implements IEnumerable<T>

Thanks for the tip! I've updated the code sample to use ANY rather than a count().

I love learning new stuff like this.
4/28/2011 11:43 AM | wtfChrisD
Gravatar

# re: QuickTip: Determine if Type implements IEnumerable<T>

:)
Linq is really a very nice tool, quite powerful :)
4/29/2011 12:53 AM | Gart
Gravatar

# re: QuickTip: Determine if Type implements IEnumerable<T>

Maybe this could be generalized into something like this:

public static bool Implements(this Type type, Type contract)
{
return contract.IsGenericTypeDefinition
? type.GetInterfaces().Any(i => i.GetGenericTypeDefinition().Equals(contract))
: type.GetInterfaces().Any(i => i.Equals(contract));
}

public static bool IsEnumerableOfT(this Type type)
{
return type.Implements(typeof (IEnumerable<>));
}

Regards
Bernhard Richter
5/3/2011 1:55 PM | Bernhard Richter
Gravatar

# Latest technology news

Its been great to discover the site it had good points and also convey its experience in its field.
1/20/2012 5:01 AM | Technology News
Gravatar

# re: QuickTip: Determine if Type implements IEnumerable<T>

I really like this website,and scene you evidence write more, thanks a lot for your information.
1/23/2012 10:30 AM | stairs nosing
Gravatar

# Hi

I really like this website,i have read your blog and find that your articles are wonderful; i have added this into my bookmark. I like it and thanks for sharing.
3/21/2012 6:43 AM | dentist framingham
Gravatar

# Nice

It is great blog, i have read your blog and find that your article are magnificent. I have added this into my bookmark. I like it and thanks for sharing.
5/29/2012 3:30 AM | body shop brampton
Gravatar

# Great

I actually like this website, and act you indication write more, thanks a lot for your info.
5/31/2012 1:51 AM | Fashion Promotional Codes
Gravatar

# re: QuickTip: Determine if Type implements IEnumerable<T>

This snippet only works against IEnumerable<> but not IEnumerable<T> like the title suggested.
3/12/2014 3:11 PM | Jason Law
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 

Powered by: