I’ve got this class with a method that returns an object:
public class Deserializer<T>
{
public static T FromJson(string json)
{
return new JavaScriptSerializer().Deserialize<T>(json);
}
}
And I’ve got a Type. How do I create an instance of the Deserializer class based on a this type? The following obviously doesn’t work:
var type = typeOf(MyObject);
var foo = Deserializer<type>.FromJson(json);
You could provide a non-generic version as well for consumers that don’t know the type at compile-time (which is a good practice by the way when you are exposing an API):
Now, consumers that know the type at compile-time:
and consumers that don’t know the type at compile-time: