I would like to have a base class that overrides ToString by converting any objects that inherits it to JSON. When running this program, it seems like this in the context of the base object is not the full object, but instead only the base object itself.
Is it possible to refer to the inherited object from the base object?
using System;
using System.Text.Json;
namespace Test
{
public class BaseModel
{
public override string ToString()
{
return JsonSerializer.Serialize(this);
}
}
public class Data : BaseModel
{
public string Name { get; set; }
public int Value { get; set; }
}
class Program
{
static void Main(string[] args)
{
var data = new Data { Name = "Test", Value = 42 };
Console.WriteLine(data);
}
}
}