如何使用UnmanagedMemoryAccessor.ReadArray <t>(Int64,T [],Int32,Int32)

时间:2017-09-14 04:55:22

标签: c# shared-memory

我想读/写数据到Memory Mapped File。如何使用UnmanagedMemoryAccessor.ReadArray(Int64,T [],Int32,Int32)

  1. struct Data {public int a; public int b; public byte [];}
  2. 静态数据_sdata = new Data(); static Data _mydata = new Data(); _mdata.byte = _sdata.byte = new byte [2];
  3. _mmf = MemoryMappedFile.CreateNew(“test”,10);
  4. var ired = _mmf.CreateViewAccessor();
  5. ired.Read(0,out mdata); here has Error 那么,T结构应该纠正什么?

1 个答案:

答案 0 :(得分:0)

数组不能是必须包含在结构中的引用。您必须为程序集启用不安全的代码并使用以下结构定义:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe struct Data {
    public int a;
    public int b;
    public fixed byte bytes[100];
}