dimanche 4 octobre 2015

How to check if type implements ICollection

How can I check if a given type is an implementation of ICollection<T>?

For instance, let's say we have the following variable:

ICollection<object> list = new List<object>();
Type listType = list.GetType();

Is there a way to identify if listType is a generic ICollection<>?

I have tried the following, but with no luck:

if(typeof(ICollection).IsAssignableFrom(listType))
       // ...

if(typeof(ICollection<>).IsAssignableFrom(listType))
      // ...

Of course, I can do the following:

if(typeof(ICollection<object>).IsAssignableFrom(listType))
     // ...

But that will only work for ICollection<object> types. If I have an ICollection<string> it will fail.





Aucun commentaire:

Enregistrer un commentaire