lundi 17 mai 2021

Reflection with Generics

Within an .Net ActionFilter I'm trying to convert the ActionArguments into its concrete type using generics. The param I'm passing in for this particular filter would be something like BaseClass<T> where T: AnotherClass. Is there anyway I can detect what 'T' is and cast my object to that particular implementation at runtime?

Here's what I'm doing now which works but I'd love to get away from using dynamic if possible:

var viewModel = filterContext.ActionArguments["viewModel"];
Type type = filterContext.ActionArguments["viewModel"].GetType();
dynamic concreteViewModel = Convert.ChangeType(entity, type);




Aucun commentaire:

Enregistrer un commentaire