views:

509

answers:

6

Is there any way to determine if an object is a generic list? I'm not going to know the type of the list, I just know it's a list. How can I determine that?

A: 

Theres a GetType() function in the System.Object class. Have you tried that?

bioskope
Event though maybe this is not a "perfect" answer, I cannot see why it is flagged minus one. Come on guys, it's not the best possible answer but try to not damage somebody's reputation.
Leandro López
GetType will return the specific generic list that it is, as opposed to the fact that it is just a generic list.
Dested
+2  A: 

Try:

if(yourList.GetType().IsGenericType)
{
  var genericTypeParams = yourList.GetType().GetGenericArguments;
  //do something interesting with the types..
}
Andrew Theken
+4  A: 

This will return "True"

List<int> myList = new List<int>();

Console.Write(myList.GetType().IsGenericType && myList is IEnumerable);

Do you care to know if it's exactly a "List"... or are you ok with it being IEnumerable, and Generic?

Timothy Khouri
+3  A: 

The following method will return the item type of a generic collection type. If the type does not implement ICollection<> then null is returned.

static Type GetGenericCollectionItemType(Type type)
{
    if (type.IsGenericType)
    {
        var args = type.GetGenericArguments();
        if (args.Length == 1 &&
            typeof(ICollection<>).MakeGenericType(args).IsAssignableFrom(type))
        {
            return args[0];
        }
    }
    return null;
}

Edit: The above solution assumes that the specified type has a generic parameter of its own. This will not work for types that implement ICollection<> with a hard coded generic parameter, for example:

class PersonCollection : List<Person> {}

Here is a new implementation that will handle this case.

static Type GetGenericCollectionItemType(Type type)
{
    return type.GetInterfaces()
        .Where(face => face.IsGenericType &&
                       face.GetGenericTypeDefinition() == typeof(ICollection<>))
        .Select(face => face.GetGenericArguments()[0])
        .FirstOrDefault();
}
Nathan Baulch
A lot of right answers, this one is the most complete, but the other one got to it first. Thanks though
Dested
A: 
if (myobj is List<object>)
{
   // code here
}
Charles Bretana
this doesn't work
Juan Manuel
A: 

The question is ambiguous.

The answer depends on what you mean by a generic list.

  • A List<SomeType> ?

  • A class that derives from List<SomeType> ?

  • A class that implements IList<SomeType> (in which case an array can be considered to be a generic list - e.g. int[] implements IList<int>)?

  • A class that is generic and implements IEnumerable (this is the test proposed in the accepted answer)? But this will also consider the following rather pathological class to be a generic list:

.

public class MyClass<T> : IEnumerable
{
    IEnumerator IEnumerable.GetEnumerator()
    {
        return null;
    }
}

The best solution (e.g. whether to use GetType, IsAssignableFrom, etc) will depend on what you mean.

Joe