如何获取下面的 HttpGet 函数来填充所有响应(由 #2 表示)?我似乎无法声明适当类型的集合来处理添加每个响应。到目前为止,我尝试过的一切都给出了编译器错误:
'无法将表达式类型 null 转换为返回类型 T`。
C#/ASP.NET/MVC 5.1/.NET 4.5
public async Task<IList<T>> Query<T>(string query)
{
var response = await _serviceHttpClient.HttpGet<IList<T>>(string.Format("query?q={0}", query), "records");
return response;
}
public async Task<T> HttpGet<T>(string urlSuffix, string nodeName)
{
// #1 var list = ???
var r = new QueryResult<T>();
do
{
r = await HttpGetInternal<T>(urlSuffix, nodeName);
// #2 list = r.Records
} while (!string.IsNullOrEmpty(r.NextRecordsUrl));
return // #3 list;
}
private async Task<QueryResult<T>> HttpGetInternal<T>(string urlSuffix, string nodeName)
{
var url = Common.FormatUrl(urlSuffix, _instanceUrl, _apiVersion);
var request = new HttpRequestMessage()
{
RequestUri = new Uri(url),
Method = HttpMethod.Get
};
request.Headers.Add("Authorization", "Bearer " + _accessToken);
var responseMessage = await _httpClient.SendAsync(request);
var response = await responseMessage.Content.ReadAsStringAsync();
if (responseMessage.IsSuccessStatusCode)
{
var jObject = JObject.Parse(response);
var jToken = jObject.GetValue(nodeName);
var res = JsonConvert.DeserializeObject<T>(jToken.ToString());
var r = new QueryResult<T>()
{
NextRecordsUrl = jObject.GetValue("nextRecordsUrl").ToString(),
Records = res
};
return r;
}
var errorResponse = JsonConvert.DeserializeObject<ErrorResponses>(response);
throw new ForceException(errorResponse[0].errorCode, errorResponse[0].message);
}
public class QueryResult<T>
{
public int TotalSize { get; set; }
public bool Done { get; set; }
public string NextRecordsUrl { get; set; }
public T Records { get; set; }
}