我需要返回一个元组响应。这是我的代码:
public Task<(List<AzureADUser> users, Dictionary<string, int> nonUserGraphObjects, string nextPageUrl, IGroupTransitiveMembersCollectionWithReferencesPage usersFromGroup)> GetNextUsersPageAsync(string nextPageUrl, IGroupTransitiveMembersCollectionWithReferencesPage usersFromGroup)
{
var users = new List<AzureADUser>();
var nonUserGraphObjects = new Dictionary<string, int>();
return (users, nonUserGraphObjects, "", null);
}
对此进行测试时,出现错误:
Tuple with 4 elements cannot be converted to type 'Task<(List<AzureADUser> users, Dictionary<string, int> nonUserGraphObjects, string nextPageUrl, IGroupTransitiveMembersCollectionWithReferencesPage usersFromGroup)>'
我错过了什么?
问题不在于元组——而是任务。您的方法声明它返回一个任务。您正在尝试直接返回值。如果您的方法是异步的,那会起作用,但事实并非如此。
也许你的意思是:
return Task.FromResult((users, nonUserGraphObjects, "", (IGroupTransitiveMembersCollectionWithReferencesPage) null));
顺便说一句,如果可以的话,我会强烈考虑为这个返回类型引入一个抽象——当一个方法的返回类型声明(即使没有这个Task<>
部分)是 159 个字符时,这对于可读性来说真的不是很好。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句