vendredi 25 août 2017

Cast object to generic list where type T taken from variable

var typeArgument = Type.GetType(mapping.TypeName);
var method = _dbAccessor.GetType()
                            .GetMethod("GetViewData")
                            .MakeGenericMethod(new Type[] { typeArgument });
var viewData = method.Invoke(_dbAccessor, new object[] { mapping.SourceName });

GetViewData is generic method. It get string data by mapping.SourceName, deserializes to List of typeArgument and returns List. viewData has type "object". But I know that viewData is List of objects of type "typeArgument". So how could i cast object to List to allow iteration by foreach for var viewData?





Aucun commentaire:

Enregistrer un commentaire