如何检查给定值是否为通用列表?

时间:2009-04-27 16:07:10

标签: c# reflection list generics

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

检查给定对象是列表还是可以转换为列表的最佳方法是什么?

9 个答案:

答案 0 :(得分:96)

对于那些喜欢使用扩展方法的人来说:

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

所以,我们可以这样做:

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

答案 1 :(得分:71)

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

}

答案 2 :(得分:12)

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

答案 3 :(得分:6)

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

答案 4 :(得分:5)

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

}

答案 5 :(得分:2)

基于Victor Rodrigues&#39;回答,我们可以为泛型设计另一种方法。事实上,原始解决方案可以简化为两行:

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

答案 6 :(得分:2)

这是一个可以在.NET Standard中运行并针对接口运行的实现:

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

这是测试(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)));
    }

答案 7 :(得分:1)

最好的方法可能就是做这样的事情:

IList list = value as IList;

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

这将为您提供最大的灵活性,并允许您使用许多实现IList接口的不同类型。

答案 8 :(得分:0)

我正在使用以下代码:

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