如何调用(不直接调用)Task <t>的ContinueWith方法?

时间:2016-07-06 09:31:10

标签: c# .net multithreading task-parallel-library

由于某些商业原因,我不能直接调用服务的方法,所以我写了这样的代码:

class TheService {
    public Task<string> CallMe(string input) {
        return Task.Run(() => {
            return "the result of " + input;
        });
    }
}

//The calling code segment...

//Get the target method's info
MethodInfo mi = typeof(TheService).GetMethod("CallMe");

//Get the target method's return type, yes, it's a Task<string>
ParameterInfo pi = mi.ReturnParameter;
Type taskType = pi.ParameterType;

//Get the service instance.(I new it here for simple reason)
TheService svc = new TheService();

//Invoke the target method and get the Task<string>, however I got only an object (not Task<string>) because I invoke it, not call it directly
object task = mi.Invoke(svc, new[] {"test"});

//How can I make a ContinueWith call here?

//This isn't work! It throws a conversion exception.
//Note: Task<string> is just an example. I wound actually get Task<MyClassA> or Task<MyClassB> here. So, I cannot hard code the conversion. However, I know the Type of Task<T> here. My instinct tells me I should use Invoke again but I don't know how to do.
((Task<object>)task).ContinueWith(a=>{
    Console.WriteLine("The result is " + a.Result);
});

我的问题是如何调用对象(它实际上是一个任务)ContinueWith方法?

或其他任何解决方法?

2 个答案:

答案 0 :(得分:4)

您可以使用基类Task

((Task)task).ContinueWith(a=>{
    Console.WriteLine("The result is " + ((dynamic)a).Result);
});

完成回调((dynamic)a).Result内部的类型为dynamic。您可以使用反射来投射或询问它。如果您愿意,可以先使用反射而不是dynamic

另一个想法:

static void Run<T>(Task<T> task) {
 ...
}

Run((dynamic)task);

这使用dynamic来匹配泛型类型参数,以便调用有效。

答案 1 :(得分:0)

这是因为您将$_FILES投射到Task<string>

Task<object>

请记住,具有不同通用约束的类型不可互换。

如果任务返回类型未知,则可以继续使用反射。

((Task<string>)task).ContinueWith(a => {
    Console.WriteLine("The result is " + a.Result);
});