Answer the question
In order to leave comments, you need to log in
How to cast a Task set with different returned results to a Task set with results as a common interface?
Good afternoon.
I am writing an asynchronous network library that has several options for returning packets: BasicStatisticResponse , FullStatisticResponse . All packages implement the common IResponse interface .
public async Task<ServerBasicStateResponse> GetBasicStatus(Server server);
public async Task<ServerFullStateResponse> GetFullStatus(Server server);
List<Task<IResponse>> requests
for later use in Task.WaitAll(requests)
.List<Task<IResponse>> serversStatisticRequests = new();
foreach(Server server in servers)
{
serversStatisticRequests.Add(service.GetBasicStatus(server));
serversStatisticRequests.Add(service.GetFullStatus(server));
}
Task<ServerBasicStateResponse>
is Task<FullStatisticResponse>
impossible to lead to Task<IResponse>
. Answer the question
In order to leave comments, you need to log in
You can wrap it up like this:
serversStatisticRequests.Add(service.GetBasicStatus(server).ContinueWith(x => (IResponse) x.Result));
serversStatisticRequests.Add(service.GetFullStatus(server).ContinueWith(x => (IResponse) x.Result));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question