I have following struct:
[StructLayout(LayoutKind.Sequential)]
struct Message
{
int Header;
int Data;
}
and I want to send it over the wire, without allocations (using SendAsync(ReadOnlyMemory<byte>)
) call.
How can I get the Memory<byte>
from given struct?
I ended up in having Span<byte>
, and then got stuck.
var message = new Message {
Header = 1, Data = 3
};
var bytes = MemoryMarshal.AsBytes(
MemoryMarshal.CreateReadOnlySpan(ref message, 1)
);
Is there any way how to put the struct directly into stream without any allocations?
I'm on netcoreapp2.1
.