日期:2014-05-18  浏览次数:21338 次

指定的类型必须是不包含引用的结构。
static void Main(string[] args)
  {
   
  string path = @"c:\MyTest93.dat";
   
  using (var mmf = MemoryMappedFile.CreateFromFile(path, FileMode.Open, "ImgA"))
  {
  using (var accessor = mmf.CreateViewAccessor(0, 32))
  {
  DataRecords dataRecords;
  dataRecords.Symbol = "ABCD";
  accessor.Write(0, ref dataRecords);//有错误提示:指定的类型必须是不包含引用的结构
  }
  }
  }

  public struct DataRecords
  {
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
  public string Symbol; 
  }


------解决方案--------------------
UnmanagedMemoryAccessor.Write<T> Method (Int64, T%)
的声明是
public void Write<T>(
long position,
ref T structure
)
where T : struct, new()

T必须是结构,也就是accessor.Write(0, ref dataRecords) dataRecords必须是个结构。

参考:
http://msdn.microsoft.com/en-us/library/dd294099.aspx
------解决方案--------------------
办法是有,不过没想到有简便的
干脆用循环,每次Write一个char