C#C
C#16mo ago
Rai

MemoryMarshal.Cast on a struct with array of known fixed size

I'm wondering if there is some way to marshal a byte array into a struct that is known to be of fixed size, or has known length values via other fields:

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;
}


Althought due to MarshalAs it should be known that it's up to 4 elements this wouldn't work due to the array being recognized as a pointer/reference type.

Is there a way to do this without having to enable unsafe and abuse the fixed keyword?
Was this page helpful?