There is a class with one method:

class A { public object Fill(HttpCLient / HttpWebRequest param) { //логика } } 

How to make it so that you can pass either the HttpCLient or HttpWebRequest to the objectFill method parameter?

I tried to do through T but failed.

thank

  • one
    do 2 methods, one with one parameter, the other with another - tym32167
  • it may not be harmful to indicate why this is the case and why you need it) so it is possible to nakostyat dynamic . - Alias

1 answer 1

Define the interface with which you will work in the Fill method and in two classes implement this interface, in one with the help of HttpClient, in the second with the help of HttpWebRequest and transfer to the Fill method instances of these classes.

Example with generic:

 public void Fill<T>(T client) { if(typeof(T) == typeof(HttpClient)) { (client as HttpClient)?.GetAsync(); } if (typeof(T) == typeof(HttpWebRequest)) { (client as HttpWebRequest)?.GetResponseAsync(); } } 
  • Thank. And with the help of the generic type one method can this be done? - Leonid Zolotarov
  • Added in response - Vadim Bondaruk
  • Thank you very much! - Leonid Zolotarov
  • By the way, instead of if (typeof(T) == typeof(HttpClient)) you can do if (client is HttpClient) =) - Groxan