5

我不确定问题的标题,但这里是:-

我有我的代码: -

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;
    }

}

这工作得很好,现在如果我需要为另一个班级做同样的事情说DogCat

我正在做的是: -

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');

这行得通吗?这是我第一次使用泛型。

4

2 回答 2

5

您的方法的定义缺少泛型类型参数。此外,您不需要第一个参数 ( input),因为您不使用它。您的方法的签名应如下所示:

private T GetAPIData<T>(string parameters, string methodToInvoke)

用法是这样的:

var testData = GetAPIData<Animal>(null, "GetAllAnimals");

该实现将T在任何地方使用使用的原始方法DogAnimal.

此外:
您的 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);
    }
}
于 2013-07-16T09:28:58.547 回答
0

您错过了通用定义

private T GetAPIData<T>(string parameters, string methodToInvoke)

var testData = GetAPIData<Animal>(null,'GetAmimals');

您的参数input无用,因此您可以将其删除。

您还可以添加类型 costraint

private T GetAPIData<T>(string parameters, string methodToInvoke) where T:IAnimal
于 2013-07-16T09:29:31.740 回答