An easy way to do this would be to define the payload as Object and then use serialization. There are tons of serializers out there so pick the one that works best for you.
public class Command
{
public string ClientUuid { get; set; }
public string Type { get; set; }
public Object Payload { get; set; }
public static void Serialize ( Command command, MemoryStream stream )
{
var formatter = new BinaryFormatter ();
formatter.Serialize ( stream, command );
}
public static void Deserialize (out Command command, MemoryStream stream )
{
var formatter = new BinaryFormatter();
command = (Command)formatter.Deserialize ( stream );
}
}
Then if typing is important, you could do something like this.
public class Command<T> : Command
{
public new T Payload
{
get
{
return (T)base.Payload;
}
set
{
base.Payload = (T)value;
}
}
}
and use it like this.
public void Usage ()
{
Command<YourObject> obj = new Command<YourObject> () {
Payload = new YourObject ()
};
using ( var stream = new MemoryStream () )
{
Command.Serialize ( obj, stream );
// do something with serialized data in stream;
}
}
CLICK HERE to find out more related problems solutions.