Skip to content

Introducing ANY as type in a generic class #3250

Answered by svick
petterek asked this question in Q&A
Discussion options

You must be logged in to vote

Effectively, what you're asking for is to make Task<T> covariant (dotnet/roslyn#2981). But classes can't be covariant in .Net, only interfaces can. Which means it would be possible to add an interface ITask<T> (dotnet/runtime#20016), which would allow you to write:

// Some code to find the method info 

// Assuming that we know that mi is a MethodInfo whose returnParameter.Type is
// some kind of Task<T>, where T is a reference type
var res = (ITask<object>)mi.Invoke(....);

return await res; 

Though if you look at the state of the two issues I linked to, it doesn't seem like this is going to happen any time soon.

Replies: 6 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by YairHalberstadt
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants
Converted from issue

This discussion was converted from issue #3250 on November 23, 2020 11:43.