124

is there a way to retrieve type T from IEnumerable<T> through reflection?

e.g.

i have a variable IEnumerable<Child> info; i want to retrieve Child's type through reflection

Usman Masood
  • 1,937
  • 2
  • 17
  • 33

13 Answers13

169
IEnumerable<T> myEnumerable;
Type type = myEnumerable.GetType().GetGenericArguments()[0]; 

Thusly,

IEnumerable<string> strings = new List<string>();
Console.WriteLine(strings.GetType().GetGenericArguments()[0]);

prints System.String.

See MSDN for Type.GetGenericArguments.

Edit: I believe this will address the concerns in the comments:

// returns an enumeration of T where o : IEnumerable<T>
public IEnumerable<Type> GetGenericIEnumerables(object o) {
    return o.GetType()
            .GetInterfaces()
            .Where(t => t.IsGenericType
                && t.GetGenericTypeDefinition() == typeof(IEnumerable<>))
            .Select(t => t.GetGenericArguments()[0]);
}

Some objects implement more than one generic IEnumerable so it is necessary to return an enumeration of them.

Edit: Although, I have to say, it's a terrible idea for a class to implement IEnumerable<T> for more than one T.

AZ_
  • 21,688
  • 25
  • 143
  • 191
jason
  • 236,483
  • 35
  • 423
  • 525
  • Or even worse write a method with yield returns and try to call GetType on a variable created with this method. It will tell you that it is not event a generic type. So basically there is no universal way to get T given an instance variable of type IEnumerable – Darin Dimitrov May 25 '09 at 12:24
  • 1
    Or try with class MyClass : IEnumerable {}. This class doesn't have a generic interface. – Stefan Steinegger May 25 '09 at 12:46
  • 1
    Why would anyone ever resort to getting the generic arguments and then grabbing the type from its indexer? That's just asking for disaster, especially when the language supports typeof(T) like @amsprich suggests in his/her answer, which can also be used with either a generic or a known type... – Robert Petz Dec 12 '13 at 22:25
  • 1
    This fails miserably when used with linq queries - the first generic argument of a WhereSelectEnumerableIterator is *not*. You're getting the generic argument of the underlying object, not the interface itself. – Pxtl Oct 31 '14 at 15:24
  • myEnumerable.GetType().GetGenericArguments()[0] gives you the FullName property which tells you the namespace.classname . If you are looking only for the class name use myEnumerable.GetType().GetGenericArguments()[0].Name – user5534263 Jun 08 '16 at 14:49
  • 2
    This fails when your using an array, which does cast to IEnumerable – Jono Jan 13 '21 at 03:10
  • 1
    For arrays, you can you GetType().GetElementType() – Jono Jan 13 '21 at 03:14
42

I'd just make an extension method. This worked with everything I threw at it.

public static Type GetItemType<T>(this IEnumerable<T> enumerable)
{
    return typeof(T);
}
Peter O.
  • 32,158
  • 14
  • 82
  • 96
amsprich
  • 429
  • 4
  • 2
36

I had a similar problem. The selected answer works for actual instances. In my case I had only a type (from a PropertyInfo).

The selected answer fails when the type itself is typeof(IEnumerable<T>) not an implementation of IEnumerable<T>.

For this case the following works:

public static Type GetAnyElementType(Type type)
{
   // Type is Array
   // short-circuit if you expect lots of arrays 
   if (type.IsArray)
      return type.GetElementType();

   // type is IEnumerable<T>;
   if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof (IEnumerable<>))
      return type.GetGenericArguments()[0];

   // type implements/extends IEnumerable<T>;
   var enumType = type.GetInterfaces()
                           .Where(t => t.IsGenericType && 
                                  t.GetGenericTypeDefinition() == typeof(IEnumerable<>))
                           .Select(t => t.GenericTypeArguments[0]).FirstOrDefault();
   return enumType ?? type;
}
Andrew
  • 4,953
  • 15
  • 40
  • 58
Eli Algranti
  • 8,707
  • 2
  • 42
  • 50
21

If you know the IEnumerable<T> (via generics), then just typeof(T) should work. Otherwise (for object, or the non-generic IEnumerable), check the interfaces implemented:

        object obj = new string[] { "abc", "def" };
        Type type = null;
        foreach (Type iType in obj.GetType().GetInterfaces())
        {
            if (iType.IsGenericType && iType.GetGenericTypeDefinition()
                == typeof(IEnumerable<>))
            {
                type = iType.GetGenericArguments()[0];
                break;
            }
        }
        if (type != null) Console.WriteLine(type);
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 3
    Some objects implement more than one generic IEnumerable. – jason May 25 '09 at 13:01
  • 5
    @Jason - and in those cases, the question of "find the T" is already a dubious question; I can't do anything about that... – Marc Gravell May 26 '09 at 04:20
  • One small gotcha for anyone trying to use this with a `Type type` parameter rather than an `object obj` parameter: you can't just replace `obj.GetType()` with `type` because if you pass in `typeof(IEnumerable)` you get nothing. To get round this, test the `type` itself to see if it is a generic of `IEnumerable<>` and then its interfaces. – Ian Mercer Mar 14 '15 at 19:19
11

Thank you very much for the discussion. I used it as a basis for the solution below, which works well for all cases that are of interest to me (IEnumerable, derived classes, etc). Thought I should share here in case anyone needs it also:

  Type GetItemType(object someCollection)
  {
    var type = someCollection.GetType();
    var ienum = type.GetInterface(typeof(IEnumerable<>).Name);
    return ienum != null
      ? ienum.GetGenericArguments()[0]
      : null;
  }
Bernardo
  • 483
  • 4
  • 8
  • Here's a one-liner that does all this using the null conditional operator: `someCollection.GetType().GetInterface(typeof(IEnumerable<>).Name)?.GetGenericArguments()?.FirstOrDefault()` – Mass Dot Net Feb 15 '19 at 02:19
3

I know this is a bit old, but I believe this method will cover all the problems and challenges stated in the comments. Credit to Eli Algranti for inspiring my work.

/// <summary>Finds the type of the element of a type. Returns null if this type does not enumerate.</summary>
/// <param name="type">The type to check.</param>
/// <returns>The element type, if found; otherwise, <see langword="null"/>.</returns>
public static Type FindElementType(this Type type)
{
   if (type.IsArray)
      return type.GetElementType();

   // type is IEnumerable<T>;
   if (ImplIEnumT(type))
      return type.GetGenericArguments().First();

   // type implements/extends IEnumerable<T>;
   var enumType = type.GetInterfaces().Where(ImplIEnumT).Select(t => t.GetGenericArguments().First()).FirstOrDefault();
   if (enumType != null)
      return enumType;

   // type is IEnumerable
   if (IsIEnum(type) || type.GetInterfaces().Any(IsIEnum))
      return typeof(object);

   return null;

   bool IsIEnum(Type t) => t == typeof(System.Collections.IEnumerable);
   bool ImplIEnumT(Type t) => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>);
}
dahall
  • 318
  • 3
  • 6
3
public static Type GetInnerGenericType(this Type type)
{
  // Attempt to get the inner generic type
  Type innerType = type.GetGenericArguments().FirstOrDefault();

  // Recursively call this function until no inner type is found
  return innerType is null ? type : innerType.GetInnerGenericType();
}

This is a recursive function that will go depth first down the list of generic types until it gets a concrete type definition with no inner generic types.

I tested this method with this type: ICollection<IEnumerable<ICollection<ICollection<IEnumerable<IList<ICollection<IEnumerable<T>>>>>>>>

which should return T

2

An alternative for simpler situations where it's either going to be an IEnumerable<T> or T - note use of GenericTypeArguments instead of GetGenericArguments().

Type inputType = o.GetType();
Type genericType;
if ((inputType.Name.StartsWith("IEnumerable"))
    && ((genericType = inputType.GenericTypeArguments.FirstOrDefault()) != null)) {

    return genericType;
} else {
    return inputType;
}
Rob Church
  • 6,783
  • 3
  • 41
  • 46
2

Just use typeof(T)

EDIT: Or use .GetType().GetGenericParameter() on an instantiated object if you don't have T.

rein
  • 32,967
  • 23
  • 82
  • 106
1

This is an improvement on Eli Algranti's solution in that it will also work where the IEnumerable<> type is at any level in the inheritance tree.

This solution will obtain the element type from any Type. If the type is not an IEnumerable<>, it will return the type passed in. For objects, use GetType. For types, use typeof, then call this extension method on the result.

public static Type GetGenericElementType(this Type type)
{
    // Short-circuit for Array types
    if (typeof(Array).IsAssignableFrom(type))
    {
        return type.GetElementType();
    }

    while (true)
    {
        // Type is IEnumerable<T>
        if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>))
        {
            return type.GetGenericArguments().First();
        }

        // Type implements/extends IEnumerable<T>
        Type elementType = (from subType in type.GetInterfaces()
            let retType = subType.GetGenericElementType()
            where retType != subType
            select retType).FirstOrDefault();

        if (elementType != null)
        {
            return elementType;
        }

        if (type.BaseType == null)
        {
            return type;
        }

        type = type.BaseType;
    }
}
Neo
  • 4,145
  • 6
  • 53
  • 76
0

this is how I usually do it (via extension method):

public static Type GetIEnumerableUnderlyingType<T>(this T iEnumerable)
    {
        return typeof(T).GetTypeInfo().GetGenericArguments()[(typeof(T)).GetTypeInfo().GetGenericArguments().Length - 1];
    }
H7O
  • 859
  • 8
  • 5
0

typeof(IEnumerable<Foo>).GetGenericArguments()[0] will return the first generic argument - in this case typeof(Foo).

Daniel Brückner
  • 59,031
  • 16
  • 99
  • 143
-1

Here's my unreadable Linq query expression version ..

public static Type GetEnumerableType(this Type t) {
    return !typeof(IEnumerable).IsAssignableFrom(t) ? null : (
    from it in (new[] { t }).Concat(t.GetInterfaces())
    where it.IsGenericType
    where typeof(IEnumerable<>)==it.GetGenericTypeDefinition()
    from x in it.GetGenericArguments() // x represents the unknown
    let b = it.IsConstructedGenericType // b stand for boolean
    select b ? x : x.BaseType).FirstOrDefault()??typeof(object);
}

Note the method also takes non-generic IEnumerable into account, it returns object in this case, because it takes a Type rather than a concrete instance as the argument. By the way, for x represents the unknown, I found this video insteresting, though it is irrelevant ..

Ken Kin
  • 4,503
  • 3
  • 38
  • 76