© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
6 replies
CEO da CloudFlare

✅ is there any way to cast Task into Task<object>?

so, i have this delegate function:

public delegate object RouterCallback(HttpRequest request);
public delegate object RouterCallback(HttpRequest request);


which some methods already implements it, and some of then are async that returns
Task<something>
Task<something>
, and i need that value inside that task.

for non Task<T> methods i can have it using:

object actionResult = matchedRoute.Callback(request);
object actionResult = matchedRoute.Callback(request);


and fine, but if it is
Task<something>
Task<something>
i can only determine if it is an task using:

if (actionResult is Task taskResult)
if (actionResult is Task taskResult)


but it doens't has the
.Result
.Result
property since it's an
Task
Task
and not
Task<T>
Task<T>


i can unsafely cast it and get it result using

 if (actionResult is Task asyncResult)
 {
     asyncResult.Wait();

     var valueTask = Unsafe.As<Task<object>>(asyncResult);
     actionResult = valueTask.Result;
     // do something with actionResult
}
 if (actionResult is Task asyncResult)
 {
     asyncResult.Wait();

     var valueTask = Unsafe.As<Task<object>>(asyncResult);
     actionResult = valueTask.Result;
     // do something with actionResult
}


but it only works when
T
T
is an reference-type and doens't works when
T
T
is an struct/value type object.

so, I need to get the value of
Task<T> actionResult
Task<T> actionResult
when actionResult is a
Task<T>
Task<T>
, but I don't know what
<T>
<T>
is. how do I managed to get it?

using reflection to get the
.Result
.Result
property value requires a lot of resources and costs a lot of performance.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Is there a way to get rid of the Task type on an object?
C#CC# / help
3y ago
❔ Any way to optimize this? Task.WhenAll
C#CC# / help
3y ago
Is there any better way to sleep thread?
C#CC# / help
3y ago