Click here to Skip to main content
15,918,516 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have a method that takes a parameter of class Type. And I want to to able pass any class name when calling this method.. and pass the class name also in var data = JsonConvert.DeserializeObject<classname>(response.Content);

//Here is method
public static void Method1(IRestResponse response, Type className)
     var data = JsonConvert.DeserializeObject<className>(response.Content);
   //I am getting error className is used like a variable but is used like a type

What I have tried:

Note sure.. I tried casting it but that did not help.
Updated 21-Jul-20 1:05am

You should use generics for this

public static void Method1<T>(IRestResponse response)
    var data = JsonConvert.DeserializeObject<T>(response.Content);

calling code;

Share this answer
If you see here: JsonConvert.DeserializeObject(T) Method (String)[^] :
Expectation is to have a class where you have written 'className'. Though you have not given much info on it, but the naming and the error seems to suggest it's a variable name and not a Type.

In method:
JsonConvert.DeserializeObject<T> Method (String)

T => The type of the object to deserialize to.

Put a type, and it should work.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900