CSharp · Console
using System;
using System.Reflection;
public class Program
{
public static void Main()
{
Type custom = typeof(CustomJson<>);
Type newInstance = custom.MakeGenericType(typeof(TestClass));
MethodInfo mi = newInstance.GetMethod("Deserialize");
var res = (TestClass) mi.Invoke(null, new object[] { "{Id: 3}" });
Console.WriteLine(res.Id);
}
}
public static class CustomJson<T> where T : class, new()
{
public static T Deserialize(string data)
{
try
{
if (string.IsNullOrEmpty(data))
return new T();
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(data);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
}
}
public class TestClass
{
public int Id { get; set; }
public TestClass()
{
}
}