Convert a byte array to a class containing a byte array in C#
- by Mathijs
I've got a C# function that converts a byte array to a class, given it's type:
IntPtr buffer = Marshal.AllocHGlobal(rawsize);
Marshal.Copy(data, 0, buffer, rawsize);
object result = Marshal.PtrToStructure(buffer, type);
Marshal.FreeHGlobal(buffer);
I use sequential structs:
[StructLayout(LayoutKind.Sequential)]
public new class PacketFormat :…