Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7160943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:28:54+00:00 2026-05-28T13:28:54+00:00

I have a task that returns a value, but I want to convert that

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:28:55+00:00Added an answer on May 28, 2026 at 1:28 pm

    I figured it out. I first created a method with the signature it’s expecting :

    public TOutput HandleTask<TTaskInput, TOutput>(TTaskInput task)
    

    This method handles the conversion and exception handling. I then use Delegate.CreateDelegate to create an instance of a delegate to this method, which I then pass into the dynamic ContinueWith invoke.

    Runs like a charm.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some example data: public struct Task { public int INT; public string
We currently have an ant task that contains something similar to the following: <filelist
I have a thread that returns a site's http response status, but sometimes my
I have an application which runs a long task and returns a value. While
I have a task that takes a rather long time and should run in
I have a Task object that has a collection of Label objects ... in
I have an MSBuild task that executes (among other things) a call to xcopy.
I have a scheduled task that is very IO intensive (deleting hundreds of thousands
I have a Fabric task that needs to access the settings of my Django
[Updated a bit] I have a Task model that has a :completed boolean attribute.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.