I work with a USB device, arrays of bytes with various data packets come from it. It is logical that you want to work with the package not as with an array of bytes, but as with a structure / class with meaningful fields.
In C ++, converting a byte array to a structure or class is very simple:
struct A { int param1; int param2; byte param3; } byte Packet[9]; //массив с пакетом, содержащим структуру А A* pA = (A*)(&Packet[0]); Is it possible in C # to convert a byte array to a structure / class in a similar way?
I really do not want for each package to write with hands the code that fills in the fields, like this:
public class ProcessorId { public UInt32 Id0 { get; private set; } public UInt32 Id1 { get; private set; } public ProcessorId(byte[] data, int offset) { Id0 = BitConverter.ToUInt32(data.Skip(offset).Take(4).Reverse().ToArray(), 0); Id1 = BitConverter.ToUInt32(data.Skip(offset + 4).Take(4).Reverse().ToArray(), 0); } } If there are a couple dozen of parameters, this is so much useless work, which in C ++ is performed in a single line.