(资料图片仅供参考)
using System;using System.IO;using System.IO.MemoryMappedFiles;using System.Runtime.InteropServices;class Program{ struct Point { private int x; private int y; public Point(int x, int y) { this.x = x; this.y = y; } public int X => x; public int Y => y; } static void Main() { int arrayLength = 10; int arraySize; unsafe { arraySize = sizeof(Point) * arrayLength; } using (var mmf = MemoryMappedFile.CreateFromFile("data.bin", FileMode.OpenOrCreate, "myData", arraySize)) { using (var accessor = mmf.CreateViewAccessor()) { unsafe { byte* ptr = null; try { accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr); // 将内存映射的指针转换为 Point 数组 Point* pointArrayPtr = (Point*)ptr; // 在 Point 数组中进行读写操作 for (int i = 0; i < arrayLength; i++) { pointArrayPtr[i] = new Point(i,i); } // 从 Point 数组中读取数据 for (int i = 0; i < arrayLength; i++) { Point value = pointArrayPtr[i]; Console.WriteLine($"x={value.X}, y={value.Y}"); } } finally { if (ptr != null) { accessor.SafeMemoryMappedViewHandle.ReleasePointer(); } } } } } }}在这个示例中,首先创建一个内存映射文件,并指定了与 `Point[]` 数组相同大小的映射区域。然后,我们使用 `CreateViewAccessor` 方法创建一个 `MemoryMappedViewAccessor` 对象。在 `unsafe` 上下文中,我们通过调用 `SafeMemoryMappedViewHandle.AcquirePointer` 方法来获取内存映射的指针,然后将指针转换为 `Point*` 类型,从而将其视为 `Point[]` 数组。通过直接使用指针进行读写操作,你可以直接修改内存映射的数据,这将自动反映在映射文件中。最后,使用 `SafeMemoryMappedViewHandle.ReleasePointer` 方法释放指针。但是,使用不安全代码需要谨慎,并且需要遵循安全性和正确性的最佳实践。确保你正确管理内存和指针,并遵循 C# 中的不安全代码规范。