[Fork] BoxGenericParameter

CSharp · Console

/Program.cs

using System;
using Newtonsoft.Json;

public class ApiResponse
{
	public int Status { get; set; }
	public string Message { get; set; }
	public object Result { get; set; }
}

public class ApiResponse<T> : ApiResponse
{
	public new T Result 
	{
		get { return (T)base.Result; }
		set { base.Result = value; }
	}
}

public class MyDTO {
	public string Text { get; set; }
}

public class Program
{
	public static void Main()
	{
		object data = new ApiResponse<MyDTO> { Result = new MyDTO { Text = "test" } };
		var serialized = JsonConvert.SerializeObject(data);		
		var deserialized = (ApiResponse<MyDTO>)JsonConvert.DeserializeObject(serialized, typeof(ApiResponse<MyDTO>));
		Console.WriteLine(((ApiResponse)deserialized).Result.GetType());		
		Console.WriteLine(JsonConvert.SerializeObject(deserialized.Result));		
	}
}
;