jeudi 5 juillet 2018

C# Casting to generic type with dynamic inner type at runtime

Type dynamicType = GetDynamicType(); //type known at runtime, but we know it inherits from BaseType
IQueryable<BaseType> query = GetQuery();
Type genericQueryable = typeof(IQueryable<>);
Type finalType = genericQueryable.MakeGenericType(dynamicType);

I want to cast variable 'query' to 'finalType' type. In other words I would like to do:

var result = query as finalType; //this is invalid code of course, or
var result = (finalType)query;

Is it possible?





Aucun commentaire:

Enregistrer un commentaire