M
Mike
The following struct, DataStruct, is only part of a larger one that contains
additional fields and arrays. I need the explicit layout because this
struct is really a union, where some of the missing fields and arrays
overlap. What's shown here, though, is sufficient for explaining the error.
290 bytes of data come from a serial device and is to be placed in this
struct. Hence, I want this struct to be 290 bytes in size, and, if I'm
adding this right, each of the fields (DataMemb1 thru DataMemb17) are 2
bytes each and the 128-element array of short values, DataArray, is 256
bytes. When I try to use the struct, however, I get the following error:
"Could not load type DataStruct from assembly <filename>,
Version=1.0.0.23184, Culture=neutral, PublicKeyToken=null because it
contains an object field at offset 34 that is incorrectly aligned or
overlapped by a non-object field."
Of course, offset 34 is where the short array, DataArray, begins. If I
manually change it to FieldOffset( 36 ), this error disappears, but I
encounter other problems because the size of the incoming data buffer, 290
bytes, is not equal to the size of the struct, which is now 292 bytes.
Can anyone shed some light on this problem? Thank you!
-Mike
[ StructLayout( LayoutKind.Explicit ) ]
public struct DataStruct
{
[ FieldOffset( 0 ) ]
public ushort DataMemb1;
[ FieldOffset( 2 ) ]
public ushort DataMemb2;
[ FieldOffset( 4 ) ]
public ushort DataMemb3;
[ FieldOffset( 6 ) ]
public ushort DataMemb4;
[ FieldOffset( 8 ) ]
public ushort DataMemb5;
[ FieldOffset( 10 ) ]
public short DataMemb6;
[ FieldOffset( 12 ) ]
public short DataMemb7;
[ FieldOffset( 14 ) ]
public ushort DataMemb8;
[ FieldOffset( 16 ) ]
public short DataMemb9;
[ FieldOffset( 18 ) ]
public short DataMemb10;
[ FieldOffset( 20 ) ]
public ushort DataMemb11;
[ FieldOffset( 22 ) ]
public short DataMemb12;
[ FieldOffset( 24 ) ]
public short DataMemb13;
[ FieldOffset( 26 ) ]
public short DataMemb14;
[ FieldOffset( 28 ) ]
public short DataMemb15;
[ FieldOffset( 30 ) ]
public ushort DataMemb16;
[ FieldOffset( 32 ) ]
public ushort DataMemb17;
[ MarshalAs( UnmanagedType.ByValArray, SizeConst = 128 ) ]
[ FieldOffset( 34 ) ]
public short[] DataArray;
}
additional fields and arrays. I need the explicit layout because this
struct is really a union, where some of the missing fields and arrays
overlap. What's shown here, though, is sufficient for explaining the error.
290 bytes of data come from a serial device and is to be placed in this
struct. Hence, I want this struct to be 290 bytes in size, and, if I'm
adding this right, each of the fields (DataMemb1 thru DataMemb17) are 2
bytes each and the 128-element array of short values, DataArray, is 256
bytes. When I try to use the struct, however, I get the following error:
"Could not load type DataStruct from assembly <filename>,
Version=1.0.0.23184, Culture=neutral, PublicKeyToken=null because it
contains an object field at offset 34 that is incorrectly aligned or
overlapped by a non-object field."
Of course, offset 34 is where the short array, DataArray, begins. If I
manually change it to FieldOffset( 36 ), this error disappears, but I
encounter other problems because the size of the incoming data buffer, 290
bytes, is not equal to the size of the struct, which is now 292 bytes.
Can anyone shed some light on this problem? Thank you!
-Mike
[ StructLayout( LayoutKind.Explicit ) ]
public struct DataStruct
{
[ FieldOffset( 0 ) ]
public ushort DataMemb1;
[ FieldOffset( 2 ) ]
public ushort DataMemb2;
[ FieldOffset( 4 ) ]
public ushort DataMemb3;
[ FieldOffset( 6 ) ]
public ushort DataMemb4;
[ FieldOffset( 8 ) ]
public ushort DataMemb5;
[ FieldOffset( 10 ) ]
public short DataMemb6;
[ FieldOffset( 12 ) ]
public short DataMemb7;
[ FieldOffset( 14 ) ]
public ushort DataMemb8;
[ FieldOffset( 16 ) ]
public short DataMemb9;
[ FieldOffset( 18 ) ]
public short DataMemb10;
[ FieldOffset( 20 ) ]
public ushort DataMemb11;
[ FieldOffset( 22 ) ]
public short DataMemb12;
[ FieldOffset( 24 ) ]
public short DataMemb13;
[ FieldOffset( 26 ) ]
public short DataMemb14;
[ FieldOffset( 28 ) ]
public short DataMemb15;
[ FieldOffset( 30 ) ]
public ushort DataMemb16;
[ FieldOffset( 32 ) ]
public ushort DataMemb17;
[ MarshalAs( UnmanagedType.ByValArray, SizeConst = 128 ) ]
[ FieldOffset( 34 ) ]
public short[] DataArray;
}