Код: Выделить всё
private byte[] ObjectToByteArray(object obj)
{
string dataType = obj.GetType().Name;
switch (dataType)
{
case "Byte[]": // an array of bytes
return (byte[])obj;
case "String": // a null-terminated ASCII string
return Encoding.ASCII.GetBytes((string)obj);
case "UInt16": // an array of unsigned short (16-bit) integers
return BitConverter.GetBytes((ushort)obj);
case "UInt32": // an array of unsigned long (32-bit) integers
return BitConverter.GetBytes((uint)obj);
case "UInt32[]": // an array of pairs of unsigned long (32-bit) integers
//return BitConverter.GetBytes((ushort)obj);
return null;
case "Int32": // an array of signed long (32-bit) integers
return BitConverter.GetBytes((int)obj);
case "Int32[]": // an array of pairs of signed long (32-bit) integers
//return BitConverter.GetBytes((int)obj);
return null;
default:
throw new Exception($"The type of value ({dataType}) is not supported.");
}
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... yte-arrays