I am attempting to use the Write<T>
function on the MemoryMappedViewAccessor
class. My T
in this case is as follows:
[StructLayout(LayoutKind.Explicit)]
public struct Message
{
public void AddString(string str)
{
if (stringContents == null)
stringContents = new byte[1024 * 10];
stringContents = Encoding.ASCII.GetBytes(str);
}
public string GetString()
{
if (stringContents == null)
return string.Empty;
return Encoding.ASCII.GetString(stringContents);
}
[FieldOffset(0)]
public byte[] stringContents;
}
However, when I make a call, such as:
//Initialized Elsewhere: MemoryMappedViewAccessor writer
Message messageAlreadyOnWire = new Message();
messageAlreadyOnWire.AddString(data);
writer.Write<Message>(0, ref messageAlreadyOnWire);
I receive an error as follows:
The specified Type must be a struct containing no references. Parameter name: type
The only 'reference' in my struct is a byte array. Is there any way to resolve this issue? I'm ok with a fixed-length byte array, but I'm not sure how to declare one without delving into the land of unsafe
, which I would prefer not to do.
As a workaround to this issue, you can use the MemoryMappedViewStream instead of the MemoryMappedViewAccessor; then use conventional stream read/write on it instead of the Accessor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With