How do I check if a given value is a generic list?

C#ReflectionListGenerics

C# Problem Overview


public bool IsList(object value)
    {
        Type type = value.GetType();
        // Check if type is a generic list of any type
    }

What's the best way to check if the given object is a list, or can be cast to a list?

C# Solutions


Solution 1 - C#

For you guys that enjoy the use of extension methods:

public static bool IsGenericList(this object o)
{
	var oType = o.GetType();
	return (oType.IsGenericType && (oType.GetGenericTypeDefinition() == typeof(List<>)));
}

So, we could do:

if(o.IsGenericList())
{
 //...
}

Solution 2 - C#

using System.Collections;

if(value is IList && value.GetType().IsGenericType) {

}

Solution 3 - C#

 bool isList = o.GetType().IsGenericType 
                && o.GetType().GetGenericTypeDefinition() == typeof(IList<>));

Solution 4 - C#

public bool IsList(object value) {
    return value is IList 
        || IsGenericList(value);
}

public bool IsGenericList(object value) {
    var type = value.GetType();
    return type.IsGenericType
        && typeof(List<>) == type.GetGenericTypeDefinition();
}

Solution 5 - C#

if(value is IList && value.GetType().GetGenericArguments().Length > 0)
{

}

Solution 6 - C#

Here's an implementation that works in .NET Standard, and works against interfaces:

    public static bool ImplementsGenericInterface(this Type type, Type interfaceType)
    {
        return type
            .GetTypeInfo()
            .ImplementedInterfaces
            .Any(x => x.GetTypeInfo().IsGenericType && x.GetGenericTypeDefinition() == interfaceType);
    }

And here are the tests (xunit):

    [Fact]
    public void ImplementsGenericInterface_List_IsValidInterfaceTypes()
    {
        var list = new List<string>();
        Assert.True(list.GetType().ImplementsGenericInterface(typeof(IList<>)));
        Assert.True(list.GetType().ImplementsGenericInterface(typeof(IEnumerable<>)));
        Assert.True(list.GetType().ImplementsGenericInterface(typeof(IReadOnlyList<>)));
    }

    [Fact]
    public void ImplementsGenericInterface_List_IsNotInvalidInterfaceTypes()
    {
        var list = new List<string>();
        Assert.False(list.GetType().ImplementsGenericInterface(typeof(string)));
        Assert.False(list.GetType().ImplementsGenericInterface(typeof(IDictionary<,>)));
        Assert.False(list.GetType().ImplementsGenericInterface(typeof(IComparable<>)));
        Assert.False(list.GetType().ImplementsGenericInterface(typeof(DateTime)));
    }

Solution 7 - C#

Based on Victor Rodrigues' answer, we can devise another method for generics. In fact, the original solution can be reduced to only two lines:

public static bool IsGenericList(this object Value)
{
    var t = Value.GetType();
    return t.IsGenericType && t.GetGenericTypeDefinition() == typeof(List<>);
}

public static bool IsGenericList<T>(this object Value)
{
    var t = Value.GetType();
    return t.IsGenericType && t.GetGenericTypeDefinition() == typeof(List<T>);
}

Solution 8 - C#

I'm using the following code:

public bool IsList(Type type) => type.IsGenericType && (
			(type.GetGenericTypeDefinition() == typeof(List<>))
			|| (type.GetGenericTypeDefinition() == typeof(IList<>))
			);

Solution 9 - C#

Probably the best way would be to do something like this:

IList list = value as IList;

if (list != null)
{
    // use list in here
}

This will give you maximum flexibility and also allow you to work with many different types that implement the IList interface.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJasonView Question on Stackoverflow
Solution 1 - C#Victor RodriguesView Answer on Stackoverflow
Solution 2 - C#James CouvaresView Answer on Stackoverflow
Solution 3 - C#Eoin CampbellView Answer on Stackoverflow
Solution 4 - C#Atif AzizView Answer on Stackoverflow
Solution 5 - C#BFreeView Answer on Stackoverflow
Solution 6 - C#Jeff SiemensView Answer on Stackoverflow
Solution 7 - C#user1618054View Answer on Stackoverflow
Solution 8 - C#Yashar AliabbasiView Answer on Stackoverflow
Solution 9 - C#Andrew HareView Answer on Stackoverflow