Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If object is Generic List

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?

like image 965
Dested Avatar asked Oct 30 '08 00:10

Dested


People also ask

How do you check if an object is in a list in C#?

public bool Contains (T item); Here, item is the object which is to be locate in the List<T>. The value can be null for reference types. Return Value: This method returns True if the item is found in the List<T> otherwise returns False.

How do you find the property of a generic type?

Use the IsGenericType property to determine whether the type is generic, and use the IsGenericTypeDefinition property to determine whether the type is a generic type definition. Get an array that contains the generic type arguments, using the GetGenericArguments method.

What is generic list in C#?

Generic List<T> is a generic collection in C#. The size can be dynamically increased using List, unlike Arrays. Let us see an example − We have set the List first − List<string> myList = new List<string>() Now add elements in the list − List<string> myList = new List<string>() { "mammals", "reptiles", "amphibians" }

Is List Object C#?

List in C# is a collection of strongly typed objects. These objects can be easily accessed using their respective index. Index calling gives the flexibility to sort, search, and modify lists if required. In simple, List in C# is the generic version of the ArrayList.


3 Answers

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?

like image 122
Timothy Khouri Avatar answered Oct 16 '22 10:10

Timothy Khouri


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();
}
like image 29
Nathan Baulch Avatar answered Oct 16 '22 08:10

Nathan Baulch


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);
}
like image 3
Stanislav Trifan Avatar answered Oct 16 '22 10:10

Stanislav Trifan