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

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

命硬 发布于 2021-11-25 字数 255 浏览 763 回复 9 原文
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?

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

从此见与不见 2022-06-07 9 楼

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.

渔村楼浪 2022-06-07 8 楼

I'm using the following code:

public bool IsList(Type type) => type.IsGenericType && (
            (type.GetGenericTypeDefinition() == typeof(List<>))
            || (type.GetGenericTypeDefinition() == typeof(IList<>))
            );
红玫瑰 2022-06-07 7 楼

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>);
}
东走西顾 2022-06-07 6 楼
if(value is IList && value.GetType().GetGenericArguments().Length > 0)
{

}
痞味浪人 2022-06-07 5 楼

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)));
    }
∞觅青森が 2022-06-07 4 楼
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();
}
雪化雨蝶 2022-06-07 3 楼
 bool isList = o.GetType().IsGenericType 
                && o.GetType().GetGenericTypeDefinition() == typeof(IList<>));
捂风挽笑 2022-06-07 2 楼
using System.Collections;

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

}
遇见了你 2022-06-07 1 楼

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())
{
 //...
}