Skip to content

Instantly share code, notes, and snippets.

@reslea
Last active December 9, 2021 17:26
Show Gist options
  • Save reslea/110dbda419cfd3193e7fb42b1b1de1aa to your computer and use it in GitHub Desktop.
Save reslea/110dbda419cfd3193e7fb42b1b1de1aa to your computer and use it in GitHub Desktop.
using System;
using Newtonsoft.Json;
public class Program
{
public static void Main()
{
var info = new Info { Some = "a", Field = 1 };
info.GetCommandJson<AddOrUpdateChat>();
}
}
public class Info
{
public string Some { get; set; }
public int Field { get; set; }
public Info() { }
}
public class BaseCommand
{
public string Data { get; set; }
}
public class AddOrUpdateChat : BaseCommand { }
public static class CommandExtentions
{
public static string GetCommandJson<T>(this object toCommand) where T : BaseCommand, new()
{
var serialized = JsonConvert.SerializeObject(toCommand);
var command = new T()
{
Data = serialized
};
return JsonConvert.SerializeObject(command);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment