Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Type.GetElementType return null?

Tags:

c#

reflection

I have a method that takes as an input parameter an object of type IEnumerable. I want to enumerate the enumeration and for each item use reflection to get the value for each property.

I have the following code:

protected void WriteData(IEnumerable data)
{
    var enumerationTypeInfo = data.GetType();
    var itemTypeInfo = enumerationTypeInfo.GetElementType();

    ...
}

The problem is enumerationTypeInfo.GetElementType() always returns null. In particular, I'm passing in a List<Entry> into WriteData, where Entry is a class I created. When I use the debugger and set a breakpoint I can see that enumerationTypeInfo correctly shows that it's a List of type Entry, but why does GetElementType return null?

Thanks

like image 664
Scott Mitchell Avatar asked Aug 15 '11 23:08

Scott Mitchell


2 Answers

GetElementType() returns the element type of arrays. List<T> is not an array type, and therefore has no "element type."

If you want to get the type of elements a random IEnumerable<T> produces, try something like this:

public static Type GetEnumerableType(Type type)
{
    if (type == null)
        throw new ArgumentNullException("type");

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

    var iface = (from i in type.GetInterfaces()
                 where i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEnumerable<>)
                 select i).FirstOrDefault();

    if (iface == null)
        throw new ArgumentException("Does not represent an enumerable type.", "type");

    return GetEnumerableType(iface);
}

Note that types can implement more than one version of the same generic interface; a type can implement both IEnumerable<int> and IEnumerable<string> for example. How you handle that case is up to you. The method I provide will take whichever interface type the runtime hands it first.


See an example using the above method on ideone.

like image 56
cdhowie Avatar answered Oct 14 '22 08:10

cdhowie


GetElementType is for use with arrays, not other generic classes. To get a generic type's generic parameters, you can use Type.GetGenericArguments.

like image 39
recursive Avatar answered Oct 14 '22 06:10

recursive