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 6329599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:41:58+00:00 2026-05-24T17:41:58+00:00

Suppose I have an API that returns a Task as an object: private static

  • 0

Suppose I have an API that returns a Task as an object:

    private static object CreateTask()
    {
        return Task.Factory.StartNew(() => "Task string");
    }

I need to extend the API with a function that returns another Task chained to the Task returned by CreateTask function so that the new Task’s result would be the result of the initial Task. I.e. something like this:

    private static object WrapTask(dynamic task)
    {
        object new_task = task.ContinueWith(parentTask => parentTask.Result, TaskContinuationOptions.ExecuteSynchronously);

        return new_task;
    }

The usage sample:

        var task = (Task<string>)WrapTask(CreateTask());

        Console.WriteLine(task.Result);

is supposed to print “Task string” string.

The problem with the WrapTask function is that the compiler refuses to accept this construction:

        object result = task.ContinueWith(parentTask => parentTask.Result, TaskContinuationOptions.ExecuteSynchronously);

with the following error:

        Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type

Would appreciate any ideas on how to go about extending the API in the described way.

  • 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-24T17:42:01+00:00Added an answer on May 24, 2026 at 5:42 pm

    You could use dynamic typing to call a generic method with type inference provided by the dynamic typing:

    private static object WrapTask(dynamic task)
    {
        return WrapTaskImpl(task);
    }
    
    private static Task<T> WrapTaskImpl<T>(Task<T> task)
    {
        return task.ContinueWith(parentTask => parentTask.Result,
            TaskContinuationOptions.ExecuteSynchronously);
    }
    

    It’s not at all clear what the point of this wrapping is, mind you… nor why you’d want to just cast the result of WrapTask – if you know that it’s a Task, why don’t you cast the result of CreateTask instead, at which point you can call WrapTask simply? Or maybe make WrapTask perform the casting itself?

    private static Task<T> WrapTask<T>(object task)
    {
        Task<T> realTask = (Task<T>) task;
        return realTask.ContinueWith(parentTask => parentTask.Result, 
                                     TaskContinuationOptions.ExecuteSynchronously);
    }
    

    Then your calling code becomes

    var task = WrapTask<string>(CreateTask());
    
    Console.WriteLine(task.Result);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you have an API you've written that collects data in real time in
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have a class module clsMyClass with an object as a member variable.
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
Suppose I have a table called Companies that has a DepartmentID column. There's also
Suppose I have a library that uses exceptions to indicate success or failure. I
Suppose I have an interface that supports a few potential operations: interface Frobnicator {
Suppose I have a stream of [acme] objects that I want to expose via
I have a function in an API I am using that request a BITMAPINFO
I have a JAR file that contains an API that uses external model files.

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.