Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if collection is of type IEnumerable<T>

How to determine if object is of type IEnumerable <T>?

Code:

namespace NS {     class Program {         static IEnumerable<int> GetInts() {             yield return 1;         }         static void Main() {             var i = GetInts();             var type = i.GetType();             Console.WriteLine(type.ToString());         }     } } 

Output:

NS.1.Program+<GetInts>d__0 

If I change GetInts to return IList, everything is OK the output is:

 System.Collections.Generic.List`1[System.Int32] 

And this returns false:

namespace NS {     class Program {         static IEnumerable<int> GetInts() {             yield return 1;         }         static void Main() {             var i = GetInts();             var type = i.GetType();             Console.WriteLine(type.Equals(typeof(IEnumerable<int>)));         }     } } 
like image 447
Valentin V Avatar asked Dec 04 '09 12:12

Valentin V


People also ask

Is IEnumerable a collection?

IEnumerable<T> is an interface that represents a sequence. Now; collections can usually be used as sequences (so... List<T> implements IEnumerable<T> ), but the reverse is not necessarily true. In fact, it isn't strictly required that you can even iterate a sequence ( IEnumerable<T> ) more than once.

What is IEnumerable type?

IEnumerable is an interface defining a single method GetEnumerator() that returns an IEnumerator interface. It is the base interface for all non-generic collections that can be enumerated. This works for read-only access to a collection that implements that IEnumerable can be used with a foreach statement.

What inherits from IEnumerable?

ICollection inherits from IEnumerable. You therefore have all members from the IEnumerable interface implemented in all classes that implement the ICollection interface.


2 Answers

If you mean the collection, then just as:

var asEnumerable = i as IEnumerable<int>; if(asEnumerable != null) { ... } 

However, I'm assuming (from the example) that you have a Type:

The object will never be "of" type IEnumerable<int> - but it might implement it; I would expect that:

if(typeof(IEnumerable<int>).IsAssignableFrom(type)) {...} 

would do. If you don't know the T (int in the above), then check all the implemented interfaces:

static Type GetEnumerableType(Type type) {     if (type.IsInterface && type.GetGenericTypeDefinition() == typeof(IEnumerable<>))         return type.GetGenericArguments()[0];     foreach (Type intType in type.GetInterfaces()) {         if (intType.IsGenericType             && intType.GetGenericTypeDefinition() == typeof(IEnumerable<>)) {             return intType.GetGenericArguments()[0];         }     }     return null; } 

and call:

Type t = GetEnumerableType(type); 

if this is null, it isn't IEnumerable<T> for any T - otherwise check t.

like image 109
Marc Gravell Avatar answered Sep 29 '22 17:09

Marc Gravell


Since IEnumerable<T> inherit IEnumerable (non generic) and if you don't need to know when a type is just IEnumerable and not IEnumerable<T> then you can use:

if (typeof(IEnumerable).IsAssignableFrom(srcType)) 
like image 33
Serge Intern Avatar answered Sep 29 '22 18:09

Serge Intern