lundi 31 octobre 2016

Casting List Type at Runtime C# Reflection

I've been working on using reflection but its very new to me still. So the line below works. It returns a list of myType

var data = (List<Mytype>)getData();

But I don't know myType until run time. So my thoughts were the below code to get the type from the object returned and cast that type as a list of myType.

var data = getData(); // returns object, at runtime
Type thisType = data.GetType().GetGenericArguments().Single(); // gets myType
var dataList = (List<thisType>)getData(); // need to return a List of myType

I'm confusing the class Type and the type used in list. Can someone point me in the right direction to get a type at run time and set that as my type for a list?





Aucun commentaire:

Enregistrer un commentaire