2013-07-16 8 views
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; 
    } 

} 

यह पूरी तरह से अच्छी तरह से काम करता है, अब अगर -

मैं के रूप में मेरे कोड है

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

    } 
-: मैं किसी अन्य वर्ग के लिए एक ही कहना Dog या Cat

क्या मैं कर रहा हूँ है क्या करने की जरूरत

अब, मैं यह है, कुछ इस नीचे की तरह एक सामान्य वर्ग के लिए इसे बदलना चाहते: -

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 का प्रयोग करेंगे हर जगह मूल विधि का इस्तेमाल किया Dog या Animal

इसके अलावा:
आपका कैच ब्लॉक कोई मूल्य नहीं जोड़ता है। वास्तव में, यह आधार अपवाद वर्ग को फेंक कर इसे हटा देता है जिसे आपको कभी फेंकना नहीं चाहिए और मूल स्टैक ट्रेस को छोड़कर। बस इसे हटा दें।

अंतिम विधि इस प्रकार दिखाई देगा:

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

: - उल्लेख 'Usage' के लिए .. धन्यवाद एक मिलियन टन ... +1, यह एक साफ तस्वीर दे दी है। – 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 
संबंधित मुद्दे