Files
PlantBox/PlantBox.Shared/Communication/Commands/CommandSerializable.cs

53 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
namespace PlantBox.Shared.Communication.Commands
{
/// <summary>
/// A class serializable to a <see cref="CommandPacket"/>
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class CommandSerializable<T> where T : new()
{
/// <summary>
/// Default <see cref="Communication.Command"/>
/// </summary>
public abstract Command Command { get; }
/// <summary>
/// Serialize all the values in a string <see cref="Array"/>,
/// should contains everything to deserialize
/// </summary>
/// <returns>Valid arguments for a <see cref="CommandPacket"/></returns>
public abstract string[] Serialize();
/// <summary>
/// Create an instance of the class from a string <see cref="Array"/>
/// </summary>
/// <param name="arguments">Arguments from the <see cref="CommandSerializable{T}.Serialize"/> method</param>
/// <returns><see cref="T"/> initilized accordingly</returns>
public abstract T Deserialize(string[] arguments);
/// <summary>
/// Convert a <see cref="CommandSerializable{T}"/> to a <see cref="CommandPacket"/>, using <see cref="Command"/>
/// </summary>
/// <param name="id">A valid id</param>
/// <returns></returns>
public CommandPacket ToCommandPacket(ulong id)
{
return new CommandPacket(Command, id, Serialize());
}
/// <summary>
/// Convert a <see cref="CommandSerializable{T}"/> to a <see cref="CommandPacket"/>
/// </summary>
/// <param name="command">Command type</param>
/// <param name="id">A valid id</param>
/// <returns></returns>
public CommandPacket ToCommandPacket(Command command, ulong id)
{
return new CommandPacket(command, id, Serialize());
}
}
}