Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The specified Type must be a struct containing no references

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.

like image 919
GWLlosa Avatar asked Nov 12 '22 20:11

GWLlosa


1 Answers

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.

like image 56
GWLlosa Avatar answered Nov 15 '22 09:11

GWLlosa