I've read several topics about but I still can't understand the real limitation of not being able to convert this structure to byte array easily:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct B {
public int b_a;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct A {
public int sizeB;
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)]
public B[] b;
}
I'm writing a TCP communication program so I want to build my S2C packets in a struct and then send them as byte[] so I'm looking for the cheapest and fastest way to achieve this.
I have already tried Marsheling in many ways but there is always some exception in Marshal.SizeOf().
In this example I get the following error: "[...] cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed."
Struct initialization eg.:
A a = new A();
B[] b = new B[5];
a.sizeB = 5;
a.b = b;
Marshal.SizeOf(a);
Marshall.SizeOf(a)which is never going to work unless your array is a fixed length.