I try to write following struct to a memory mapped file, but I still have problem with the array (writing throws exception that the struct can not contain reference)
[StructLayout(LayoutKind.Explicit)]
struct IndexEntry {
[FieldOffset(0)]
public byte key;
[FieldOffset(4)]
public int lastValueIdx;
[FieldOffset(8)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = Constants.PART_ENTRY_SIZE)]
public long[] values;
}
I use this calling for writing:
UnmanagedMemoryAccessor.Write<IndexEntry>(0, ref entry);
Can you please tell me, what am I doing wrong? Thx
The solution of this is using the fixed size array and unsafe code. So the struct should look like this:
Note that the program (or a single project containing that struct) must be compiled with the "/unasfe" option and the array must be then accessed like this:
Then the
UnmanagedMemoryAccessor.Write<T>(...)
andUnmanagedMemoryAccessor.Read<T>(...)
functions work perfectly.