c#
  • generics
  • 2013-07-16 10 views 5 likes 
    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; 
         } 
    
        } 
    

    現在,我想這將其更改爲通用類,像下面這樣: -

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

    這是否會工作...這是我第一次使用泛型。

    回答

    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); 
        } 
    } 
    
    +1

    : - 謝謝!A萬噸... +1提'Usage',它給了一個清晰的畫面。 – Shubh

    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 
    
    相關問題