title: How to pass "unknown" object as parameter and use it's original object type to deserialize
body: <p>I am working on building a re-usable function to deserialize a JSON string.</p>
  <p>The JSON deserialization routines need an object type(jsonTestObject). This all works as expected.</p>
  <pre><code>  // serialize json to object
  
    jsonTestObject testO = new jsonTestObject();
    testO = Newtonsoft.Json.JsonConvert.DeserializeObject&lt;jsonTestObject&gt;(jsonString);
  </code></pre>
  <p>When I try to wrap this in a re-usable function to deserialize &quot;any&quot; object type I send, I run into issues. I am not sure how to pass the object type as a parameter to the function as I cant strongly type it.</p>
  <p>I want to send &quot;any&quot; object type to my function and cast the result to my desired object type (As I will know what object type I am expecting when making the call.)</p>
  <p>I don't want to hardcode a separate function for all classes that I want to serialize/deserialize.</p>
  <p>Here is what I have that is not working...</p>
  <pre><code>    JSONdeserialize(string jSONstring,Type t)
      {
          object newObject;
          
          // deserialize object based on the original object type that was passed in.
          newObject = JsonConvert.DeserializeObject&lt;t&gt;(jSONstring); //ERROR LINE
         
          return newObject;
      }
  
  jsonTestObject jsonObject = new jsonTestObject();
  object o = JSONdeserialize(jsonString, jsonObject.GetType());
  
  jsonObject = (jsonTestObject)o;
  </code></pre>
  <blockquote>
  <p>ERROR: &quot;'t' is a variable but is used like a type&quot;</p>
  </blockquote>