gpt4 book ai didi

c# - 返回空 T

转载 作者:行者123 更新时间:2023-11-30 14:25:33 25 4
gpt4 key购买 nike

<分区>

我无法在此泛型类型 T 上返回 null。我已尝试将其标记为 Nullable、Nullable 或 T?没有成功...

此方法是抽象类的一部分,我需要使其尽可能通用,以便我可以检索任何类型的对象并从任何派生类中使用它。

public T GetFromApi<T>(string apiRequest, string content)
{
try
{
log.Debug("Requesting '" + apiRequest + "' from API with the following parameters: " + content);
_httpClient.DefaultRequestHeaders.Accept.Clear();
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_httpClient.DefaultRequestHeaders.Add("Authorization", string.Format("{0} {1}", _token.token_type, _token.access_token));

var httpContent = new StringContent(content);
HttpResponseMessage response = _httpClient.GetAsync(apiRequest).Result;

if (response.IsSuccessStatusCode)
{
//Returns requested Object (T) from API
log.Info("Returning requested object " + typeof(T).ToString());
return response.Content.ReadAsAsync<T>().Result;
}
else
{
log.Error("Error accessing API.");
return null;
}
}
catch (Exception ex)
{
log.Fatal("Error accessing API.", ex);
throw ex;
}
}

else 语句的 return null 给我错误:

Cannot convert null to type parameter 'T'

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com