我在异步方法中返回变量时遇到麻烦。我可以执行该代码,但是无法返回该电子邮件地址。
public async Task<string> GetSignInName (string id)
{
RestClient client = new RestClient("https://graph.windows.net/{tenant}/users");
RestRequest request = new RestRequest($"{id}");
request.AddParameter("api-version", "1.6");
request.AddHeader("Authorization", $"Bearer {token}");
//string emailAddress = await client.ExecuteAsync<rootUser>(request, callback);
var asyncHandler = client.ExecuteAsync<rootUser>(request, response =>
{
CallBack(response.Data.SignInNames);
});
return "test"; //should be a variable
}
答案 0 :(得分:1)
RestSharp内置了用于执行基于任务的异步模式(TAP)的方法。这是通过public void PPLChoiceOption(int selection)
{
buttons[selection].GetComponent<Button>().interactable = false;
PPLTextBox.GetComponent<Text>().text = hints[numberOfWrongAnswers - 1];
showRandomHintImage();
PPLChoiceMade = selection;
}
方法调用的。这将给您一个响应,并且RestClient.ExecuteTaskAsync<T>
属性将具有通用参数的反序列化版本(在您的情况下为rootUser)。
response.Data
请注意,public async Task<string> GetSignInName (string id)
{
RestClient client = new RestClient("https://graph.windows.net/{tenant}/users");
RestRequest request = new RestRequest($"{id}");
request.AddParameter("api-version", "1.6");
request.AddHeader("Authorization", $"Bearer {token}");
var response = await client.ExecuteTaskAsync<rootUser>(request);
if (response.ErrorException != null)
{
const string message = "Error retrieving response from Windows Graph API. Check inner details for more info.";
var exception = new Exception(message, response.ErrorException);
throw exception;
}
return response.Data.Username;
}
对于C#中的类不是一个好名字。我们的常规约定是PascalCase类名,因此它应该是RootUser。