Last active
December 9, 2021 17:26
-
-
Save reslea/110dbda419cfd3193e7fb42b1b1de1aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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