using System.Collections.Concurrent;
public static void Main()
var test = new NewGreetingAddedSimple() as JsonMessage;
var test2 = new NewGreetingAddedSimple2() as JsonMessage;
Console.WriteLine(test2.Type);
Console.WriteLine(JsonSerializer.Serialize( new TransportCompanySettings()));
public class TransportCompanySettings
public bool DPD { get; set; }
public bool Dostavista { get; set; }
public bool PecEasyway { get; set; }
public bool SberLogistics { get; set; }
public bool Technopark { get; set; }
public abstract class JsonMessage
private readonly string _type;
private readonly string _version;
public virtual string Type => _type;
public virtual string Version => _version;
public abstract string JsonBody { get; }
protected JsonMessage(Type type)
var (t,v) = MessageTypesDictionary.GetTypeVersion(type);
public class NewGreetingAddedSimple : JsonMessage
public int ReceiverUserId { get; set; }
public int SenderUserId { get; set; }
public DateTime AddedAt { get; set; } = DateTime.Now;
public override string JsonBody => new {ReceiverUserId, SenderUserId, AddedAt}.ToString();
public NewGreetingAddedSimple() : base(typeof(NewGreetingAddedSimple))
public class NewGreetingAddedSimple2 : JsonMessage
public int SenderUserId { get; set; }
public override string JsonBody => new {SenderUserId}.ToString();
public NewGreetingAddedSimple2() : base(typeof(NewGreetingAddedSimple2))
public static class MessageTypesDictionary
private static readonly ConcurrentDictionary<Type, (string, string)> _storage = new ConcurrentDictionary<Type, (string, string)>();
public static (string, string) GetTypeVersion(Type type)
if (_storage.TryGetValue(type, out (string, string) foundRecord))
Console.WriteLine("Fetched from dict");
return (foundRecord.Item1, foundRecord.Item2);
if(_storage.TryAdd(type, (type.FullName, type.Assembly.GetName().Version.ToString())))
Console.WriteLine("Added to dict");
Console.WriteLine("something went wrong");
return (type.FullName, type.Assembly.GetName().Version.ToString());