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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/794198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 01:26:06  来源:igfitidea点击:

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

c#reflectionlistgenerics

提问by

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?

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

采纳答案by James Couvares

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

}

回答by Andrew Hare

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 IListinterface.

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

回答by BFree

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

}

回答by Eoin Campbell

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

回答by Atif Aziz

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

回答by Victor Rodrigues

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

回答by James M

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

基于 Victor Rodrigues 的回答,我们可以为泛型设计另一种方法。事实上,原来的解决方案可以简化为只有两行:

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

回答by Jeff Siemens

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

这是一个适用于 .NET Standard 并且适用于接口的实现:

    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):

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

回答by Yashar Aliabbasi

I'm using the following code:

我正在使用以下代码:

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