If object is Generic List

前端 未结 6 409
既然无缘
既然无缘 2021-01-11 10:32

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?

相关标签:
6条回答
  • 2021-01-11 10:41

    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.

    0 讨论(0)
  • 2021-01-11 10:46

    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();
    }
    
    0 讨论(0)
  • 2021-01-11 10:50

    Try:

    if(yourList.GetType().IsGenericType)
    {
      var genericTypeParams = yourList.GetType().GetGenericArguments;
      //do something interesting with the types..
    }
    
    0 讨论(0)
  • 2021-01-11 10:50

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

    0 讨论(0)
  • 2021-01-11 10:54

    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);
    }
    
    0 讨论(0)
  • 2021-01-11 11:06

    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?

    0 讨论(0)
提交回复
热议问题