mardi 5 juillet 2022

cast object Task

I'm making a unit test class with a config file. I have similar object (same interfaces) to check.

At a point of my code I get Task<ActionResult<OneOfMyObjectType>> object by using reflection. my issue is to read the Result I have to cast this Object First but I can't use Task<IActionResult> or Task<ActionResult<InferfaceOfTheObject>>. the compiler only allow me to cast to the Specific class that is used. (if I don't I have an InvalidCastException).

How Can I do this?

extract of code of my tests :

dynamic objectType = Type.GetType($"{ClassName},{Assembly}")!;
Type ControllerActionType = Type.GetType($"{ControllerActionsClasseName}, {Assembly}")!;
MethodInfo info = ControllerActionType.GetMethod(methodName!)!;
var myInstance = Activator.CreateInstance(ControllerActionType, new object[] { context });
var testObject = JsonConvert.DeserializeObject(operation!.Data, objectType);
var actionResult = info!.Invoke(myInstance, new object[] { testObject })!;
var castedActionResult = "** I'm blocked here**";




Aucun commentaire:

Enregistrer un commentaire