0

如何获取下面的 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; }
}
4

1 回答 1

0

我会更改签名以返回IList<T>与调用它的方法相同的签名,因为没有一种简单的方法可以创建实现泛型指定的接口的东西。另一种选择是用一个具体的类型来调用它,比如List<T>,它实现了调用者的返回接口,但我认为另一种方式更简洁。

由于r.Records是类型T,它应该能够添加到List<T>. List<T>实现IList<T>并因此满足返回类型。

public async Task<IList<T>> Query<T>(string query)
{
    var response = await _serviceHttpClient.HttpGet<T>(string.Format("query?q={0}", query), "records");
    return response;
}

public async Task<IList<T>> HttpGet<T>(string urlSuffix, string nodeName)
{
    var list = new List<T>();

    do
    {
        var r = await HttpGetInternal<T>(urlSuffix, nodeName);

        list.Add(r.Records);
    }
    while (!string.IsNullOrEmpty(r.NextRecordsUrl));

    return list;
}
于 2014-01-30T03:16:11.813 回答