How to determine if a type is a type of collection?

Really all of these types inherit IEnumerable. You can check only for it:

bool IsEnumerableType(Type type)
{
    return (type.GetInterface(nameof(IEnumerable)) != null);
}

or if you really need to check for ICollection:

bool IsCollectionType(Type type)
{
    return (type.GetInterface(nameof(ICollection)) != null);
}

Look at “Syntax” part:

  • List<T>

  • IList

  • ICollection

If you need to exclude strings (which are essentially an IEnumerable<char>), use the following function:

bool IsEnumerableType(Type type)
{
    return (type.Name != nameof(String) 
        && type.GetInterface(nameof(IEnumerable)) != null);
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)