Created
April 24, 2012 23:31
-
-
Save nelsonlaquet/2484572 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 System.Linq; | |
using System.Text; | |
using Newtonsoft.Json; | |
namespace ConsoleApplication146 | |
{ | |
interface ICommand | |
{ | |
void Display(); | |
} | |
class BoardPosition | |
{ | |
public int Row { get; private set; } | |
public int Col { get; private set; } | |
public BoardPosition(int row, int col) | |
{ | |
Row = row; | |
Col = col; | |
} | |
} | |
class DoStuffCommand : ICommand | |
{ | |
public string Whoa { get; private set; } | |
public int Blegh { get; private set; } | |
public BoardPosition Position { get; private set; } | |
public DoStuffCommand(string whoa, int blegh, BoardPosition position) | |
{ | |
Whoa = whoa; | |
Blegh = blegh; | |
Position = position; | |
} | |
public void Display() | |
{ | |
Console.WriteLine(string.Format("DoStuff - {0}, {1}, Pos: {2}, {3}", Whoa, Blegh, Position.Row, Position.Col)); | |
} | |
} | |
class Program | |
{ | |
static void Main() | |
{ | |
// client side | |
var cmd = new DoStuffCommand("Hey there", 42, new BoardPosition(1, 3)); | |
cmd.Display(); | |
var bytes = Save(cmd); | |
var type = "ConsoleApplication146.DoStuffCommand"; | |
// server side | |
var serverCmd = Load(bytes, type); | |
serverCmd.Display(); | |
Console.ReadKey(); | |
} | |
static ICommand Load(byte[] bytes, string type) | |
{ | |
return (ICommand) JsonConvert.DeserializeObject(Encoding.UTF8.GetString(bytes), Type.GetType(type)); | |
} | |
static byte[] Save(ICommand command) | |
{ | |
return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(command, Formatting.Indented)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment