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:
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?
3 Replies
MarshalAs has no relationship to MemoryMarshal
you either need to use a fixed buffer or an inline array. it is impossible to make this work with
int[]
in a "free" way. you could get it to work using Marshal.PtrToStructure
but it is the same as manually assigning instances of the struct in a loopThank you very much I completely forgot that the InlineArray attribute exists. This will make life easier.