I am not sure about the title of the question, but here it is:-
I have my code as :-
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;
}
}
This works perfectly well, now if I need to do the same for another class say Dog
or Cat
What I am doing is:-
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;
}
}
Now, I want it to change it to a Generic class , something like this below:-
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;
}
But, I am not able to do it. Puzzled even how will I call this method?
var testData = GetAPIData(new Aminal(),null,'GetAmimals');
Will this work... This is the first time I am working with generics.
The definition of your method is missing the generic type parameter. Additionally, you don't need the first parameter (input
), because you don't use it. The signature of your method should look like this:
private T GetAPIData<T>(string parameters, string methodToInvoke)
Usage would be like this:
var testData = GetAPIData<Animal>(null, "GetAllAnimals");
The implementation would use T
everywhere the original method used Dog
or Animal
.
Furthermore:
Your catch block doesn't add any value. In fact, it removes it by throwing the base exception class that you should never throw and by discarding the original stack trace. Just remove it.
The final method would look like this:
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With