mardi 16 juillet 2019

Given a type variable representing Foo, can you cast a (valid) object to type IList

This question already has an answer here:

I'm aware of at least one way to cast an object to a type defined by a Type variable.

Convert.ChangeType(obj o, Type t);

However I'm not sure at all how to (if it is possible) cast it to a type using the type variable in a generic parameter.

Could you achieve something like this?

//Type t exists and represents Foo type
//object o1 exists and contains a valid List<Foo>

var casted = Convert.ChangeType(o1, IList<t>); //doesn't compile as expected

//do work with casted which is now IList<foo>





Aucun commentaire:

Enregistrer un commentaire