mercredi 4 avril 2018

How do I check if an object is an ICollection

I've tried the following:

if ((type == typeof(ICollection<T>)))

if (type.GetInterfaces().Contains(System.Collections.Generic.ICollection<T>))

Neither of these seem to catch anything at all, even things that I know are ICollections. If I try to look for IEnumerable, then it catches too many things, including strings.

There was another answer on SO that said to use this:

if(Obj is ICollection)
{
    //Derived from ICollection
}

That doesn't even compile for me. I have to specify that it's generic. When I look at the object type in the debugger, this is what I see:

{Name = "ICollection`1" FullName = "System.Collections.Generic.ICollection`1[[object1, object1Namespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"}

I don't know how to look for that type. What I want is any type that is specifically an ICollection of any type.





Aucun commentaire:

Enregistrer un commentaire