c# - Passing a data class as a variable to a function -
I am trying to create a reusable module for API calls in C #. I want to use it for many different APIs. But there is a problem passing my data class as my function, which handles the API call.
So we say, for example, that I say two completely separate APIs, we call them "AP # 1" and "API # 2". And I have the following classes:
// The format of the response from API # 1 is the public class, its 01Get {Public Entity ID {get; Set; } Public Date Time Order Date {Received; Set; }} // This is AP # 2 Public Sector API 02 Gate {Public Bull Hagreen {Received; Set; } The name of the public string {get; Set; }}
Here's a call to my function. It works fine if I use one of my data squares directly in this way:
var results = callap
But this does not allow me flexibility.
Here is the definition of my function (I do not affect those who are leaving other parameters things):
private async Task & lt; TResult & gt; CallAPI & lt; TResult & gt; (Uri Besadder, string request Yuri) {(using a client (client. Base Adder = Besadder; client.DefaultRequestHeaders.Accept.Clear (); Client.DefaultRequestHeaders.Accept.Add (new MediaTypeWithQualityHeaderValue ( "application / Jason")) ; Acteepiarpispenshn Send feedback = new Actiteepiarpispasmess (); response = wait client.GetAsync (requestUri); if (ResponsekIsSuccessStatusCode) wait {TResult result = response. content. Reedassink & lt; TRTTULT & gt; ();} } // do other stuff ... N results;}
Now this is where I got hung up. I want to think the wrong way about it, but I'm certain that I am trying to do is set an example is:
object Apimodel; switch (Jeapiai) {case "API_01_GET": Apimodel = new Api 0lGet (); break; breakage case "API_02_GET": Apimlod = new API 022 (); // Other cases ... Default: break;} Var result = call API (baseAdder, Request Yuri);
end I receive an error when I try to line up "type or namespace name 'apmodel' could not be found (are you not using access director or assembly reference?)". I have tried other things, but this is the version that seemed to be the closest to me, so this is what I posted.
That is why I am unsure of fulfilling it or there is a better way completely. I appreciate any feedback and would be happy to clarify if necessary. Unfortunately, you can not use generic here without a few additional reflections to call dynamically Fortunately, there are additional overloads that will allow you to enter you may need to change some of its code before although you Thank you in advance.
type
. You just need to surrender your method and make a small tweak for ReadAsAynync
.
Private async Tasks & lt; Object & gt; CallAPI (Uri Bassder, String Request URI, Type APIType) {(Using Client (Client = New HTTP)} {.... (Response .usuvstustus code) {Object Result = response awaiting content. Reedassink (API type);}} .... return result;}
object
Are associated with.
Comments
Post a Comment