Dested
Dested

Reputation: 6433

If object is Generic List

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?

Upvotes: 15

Views: 18575

Answers (6)

Stanislav Trifan
Stanislav Trifan

Reputation: 154

The accepted answer doesn't guarantee the type of IList<>. Check this version, it works for me:

private static bool IsList(object value)
{
    var type = value.GetType();
    var targetType = typeof (IList<>);
    return type.GetInterfaces().Any(i => i.IsGenericType 
                                      && i.GetGenericTypeDefinition() == targetType);
}

Upvotes: 3

Nathan Baulch
Nathan Baulch

Reputation: 20683

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();
}

Upvotes: 7

to StackOverflow
to StackOverflow

Reputation: 124686

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.

Upvotes: 0

Timothy Khouri
Timothy Khouri

Reputation: 31845

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?

Upvotes: 23

Andrew Theken
Andrew Theken

Reputation: 3480

Try:

if(yourList.GetType().IsGenericType)
{
  var genericTypeParams = yourList.GetType().GetGenericArguments;
  //do something interesting with the types..
}

Upvotes: 2

bioskope
bioskope

Reputation: 323

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

Upvotes: -1

Related Questions