question

FedericoLunasalazar-8529 avatar image
0 Votes"
FedericoLunasalazar-8529 asked ChaoDeng-MSFT edited

How to pass and receive a type as a parameter

Hello.

How to pass a type to a method as a parameter?

I have this method:

             private string CallApi(string api, string method)
             {
                 var client = new RestClient(api);
                 var request = new RestRequest(method, DataFormat.Json);
    
                 var response = client.Get(request);
    
                 Employee obj = JsonConvert.DeserializeObject<Employee>(response.Content);
    
                 return obj.Descrip;
             }

What I want is to receive the type Employee or any other as a parameter so the line Employee obj = ... looks like:

type obj = JsonConvert.DeserializeObject<type>(response.Content);

How to send Employee type as parameter to the method CallAPI:
string CallApi("api", "method", Employee)

Employee is a Model in my project.

Thanks in advance.


dotnet-aspnet-core-general
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @FedericoLunasalazar-8529 ,

Do you mean you want to use generics as passing parameters?

0 Votes 0 ·

0 Answers