简体   繁体   English

投射对象任务<ActionResult<T> &gt; 其中 T 是类型变量

[英]cast object Task<ActionResult<T>> where T is Type variable

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.在我的代码中,我通过使用反射获得了Task<ActionResult<OneOfMyObjectType>>对象。 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>> .我的问题是阅读结果我必须先转换这个对象,但我不能使用Task<IActionResult>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 ). (如果我没有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**";

Without more information, my guess is that Controller.Method returns a Task , and that you're trying to cast the result from the method as Task<ActionResult> .如果没有更多信息,我的猜测是Controller.Method返回一个Task ,并且您正在尝试将该方法的结果转换为Task<ActionResult>

All you need to here is two casts, one that matches the return type from the method, and one on the Task's result that is the expected underlying type.您需要做的只是两种转换,一种与方法的返回类型匹配,另一种与预期的基础类型的任务结果相匹配。

var actionResultTask = (Task<IActionResult>)info!.Invoke(myInstance, new object[] { testObject })!;
var castedActionResult = (ActionResult<InferfaceOfTheObject>) = actionResultTask.Value;

The point here, is that without a lot more reflection, the generic parameters must match.这里的要点是,没有更多的反射,泛型参数必须匹配。 So if the ActionResult from method was declared with the interface, then it has to be casted ActionResult<InterfaceOfTheObject> , if it was declared using an implementation then that cast has to be used instead.因此,如果方法中的 ActionResult 是使用接口声明的,则必须将其转换为ActionResult<InterfaceOfTheObject> ,如果它是使用实现声明的,则必须使用该转换。

If you're unsure of a type of a property, but are confident that its return type implements a relevant interface, then you can use the following method to get the property value by reflection.如果您不确定某个属性的类型,但确信其返回类型实现了相关接口,那么您可以使用以下方法通过反射获取属性值。 You should be able to use this to get any property value.您应该能够使用它来获取任何属性值。

public void SomeTestMethod() {
    /* ... */
    var methodResult = (Task)info.Invoke(myInstance, new object[] {});
    var actionResult = GetGenericPropertyValue<IActionResult>(methodResult, "Result");
    var actionResultValue = GetGenericPropertyValue<InferfaceOfTheObject>(actionResult, "Value");
}
    
public static T GetGenericPropertyValue<T>(object obj, string propertyName)
{
    return (T)obj.GetType().GetProperty(propertyName).GetValue(obj);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM