我不确定问题的标题,但这里是: -
我的代码为: -
HttpClient client = new HttpClient();// Create a HttpClient
client.BaseAddress = new Uri("http://localhost:8081/api/Animals");//Set the Base Address
//eg:- methodToInvoke='GetAmimals'
//e.g:- input='Animal' class
HttpResponseMessage response = client.GetAsync('GetAllAnimals').Result; // Blocking call!
if (response.IsSuccessStatusCode)
{
XmlSerializer serializer = new XmlSerializer(typeof(Animal));//Animal is my Class (e.g)
string data = response.Content.ReadAsStringAsync().Result;
using (MemoryStream ms = new MemoryStream(UTF8Encoding.UTF8.GetBytes(data)))
{
var _response = (Animal)serializer.Deserialize(ms);
return _response;
}
}
这非常有效,现在如果我需要为另一个班级做同样的事情,请说Dog
或Cat
我在做的是: -
HttpClient client = new HttpClient();// Create a HttpClient
client.BaseAddress = new Uri("http://localhost:8081/api/Animals");//Set the Base Address
//eg:- methodToInvoke='GetAmimals'
//e.g:- input='Animal' class
HttpResponseMessage response = client.GetAsync('GetAllDogs').Result; // Blocking call!
if (response.IsSuccessStatusCode)
{
XmlSerializer serializer = new XmlSerializer(typeof(Dog));//Animal is my Class (e.g)
string data = response.Content.ReadAsStringAsync().Result;
using (MemoryStream ms = new MemoryStream(UTF8Encoding.UTF8.GetBytes(data)))
{
var _response = (Dog)serializer.Deserialize(ms);
return _response;
}
}
现在,我希望它将其更改为Generic类,如下所示: -
private T GetAPIData(T input,string parameters, string methodToInvoke)
{
try
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:8081/api/Animals");
//eg:- methodToInvoke='GetAmimals'
//e.g:- input='Animal' class
HttpResponseMessage response = client.GetAsync(methodToInvoke).Result; // Blocking call!
if (response.IsSuccessStatusCode)
{
XmlSerializer serializer = new XmlSerializer(typeof(input));
string data = response.Content.ReadAsStringAsync().Result;
using (MemoryStream ms = new MemoryStream(UTF8Encoding.UTF8.GetBytes(data)))
{
var _response = (input)serializer.Deserialize(ms);
return _response;
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return (T)input;
}
但是,我无法做到。甚至困惑我怎么称呼这种方法?
var testData = GetAPIData(new Aminal(),null,'GetAmimals');
这会有效......这是我第一次使用泛型。
答案 0 :(得分:5)
您的方法的定义缺少泛型类型参数。此外,您不需要第一个参数(input
),因为您不使用它。您的方法的签名应如下所示:
private T GetAPIData<T>(string parameters, string methodToInvoke)
用法如下:
var testData = GetAPIData<Animal>(null, "GetAllAnimals");
在原始方法使用T
或Dog
的任何地方,实施都会使用Animal
。
此外:
您的catch块不会添加任何值。实际上,它通过抛出您不应该抛出的基本异常类并通过丢弃原始堆栈跟踪来删除它。只需删除它。
最终方法如下所示:
private T GetAPIData<T>(string parameters, string methodToInvoke)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:8081/api/Animals");
//eg:- methodToInvoke='GetAmimals'
//e.g:- input='Animal' class
HttpResponseMessage response = client.GetAsync(methodToInvoke).Result;
if (!response.IsSuccessStatusCode)
throw new InvalidOperationException("Request was not successful");
XmlSerializer serializer = new XmlSerializer(typeof(T));
string data = response.Content.ReadAsStringAsync().Result;
using (MemoryStream ms = new MemoryStream(UTF8Encoding.UTF8.GetBytes(data)))
{
return (T)serializer.Deserialize(ms);
}
}
答案 1 :(得分:0)
您错过了通用定义
private T GetAPIData<T>(string parameters, string methodToInvoke)
和
var testData = GetAPIData<Animal>(null,'GetAmimals');
您的参数input
无效,因此您可以删除它。
您还可以添加type costraint:
private T GetAPIData<T>(string parameters, string methodToInvoke) where T:IAnimal