21 lines
723 B
C#
21 lines
723 B
C#
// Licensed to Timothy Schenk under the GNU AGPL Version 3 License.
|
|
|
|
using System.Runtime.InteropServices;
|
|
using System.Text.Json;
|
|
|
|
Span<byte> buffer = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
|
|
Console.WriteLine(buffer.Length == Marshal.SizeOf<TestStruct>());
|
|
var tstruct = MemoryMarshal.Cast<byte, TestStruct>(buffer)[0];
|
|
Console.WriteLine(JsonSerializer.Serialize(tstruct));
|
|
Console.WriteLine(tstruct.b);
|
|
|
|
[StructLayout(LayoutKind.Explicit, Size = 20)]
|
|
public struct TestStruct {
|
|
[FieldOffset(0)]
|
|
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I4, SizeConst = 4)]
|
|
public int[] arr;
|
|
|
|
[FieldOffset(16)]
|
|
[MarshalAs(UnmanagedType.I4)]
|
|
public int b;
|
|
}
|