Question

I have a task that returns a value, but I want to convert that value to something else (for example, from string to int). This is normally very easy to do, all I do is add continuation task which does the conversion and returns the new type as follows :

ConverterService converter = ...;
Task<string> originalTask = Task.Factory.FromAsync<string>(...);
Task<int> conversionTask = originalTask.ContinueWith(p => converter.Convert(typeof(string), typeof(int), p.Result));

Problem is the types are unknown :( I have been able to generate the originalTask dynamically. Here is an excerpt off the top of my head :

ConverterService converter = ...;
// dynamically calling Task.Factory.FromAsync
var originalTask = FromAsyncMethodInfo.Invoke(Task.Factory, args.ToArray());
...

// now I want to dynamically call Task<string>.ContinueWith
var conversionTask = ContinueWithMethodInfo.Invoke(originalTask, ???)

What do I do now? I expect to supply it with a Func<Task<T>, U> (which is really Func<Task<string>, int> in this example), but how do I generate this dynamically?

To keep things simple, I just want to know how to dynamically create a Func<T> at runtime when all I have is a Type variable. Either that or a dynamically generated alternative to the lambda seen in the first block of code :

Task<int> conversionTask = originalTask.ContinueWith(p => converter.Convert(typeof(string), typeof(int), p.Result));

Many thanks in advance.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top