jeudi 10 décembre 2015

How to get the underlying type of an IList item?

I have a method that receives an IList. Is there a way to get the Type of the items of the IList?

public void MyMethod(IList myList)
{
}

I have a class that you can associate an IList to, you can also add a NewItem function, but I would like to be able to add items with the default empty constructor in case the user didn't set the NewItem function.

How can I get the Type of the underlying items? I would know how to do it if it was an IList<T>, but I can't change the API, because I can receive any kind of collection that implements IList, only restriction, that is not enforced by code, is that all the items in the collections we receive are of the same type.





Aucun commentaire:

Enregistrer un commentaire