不安全的 C# - 将不安全的指针传递给方法

Unsafe C# - passing an unsafe pointer to a method

我在 class 的方法之一中收到一个指向二维数据集的不安全 byte* 类型指针。我想在同一个 class 中定义另一个方法,它将对该指针进行一些指针运算。当我尝试按如下方式定义方法时,出现了几个编译时错误。看起来 class 范围内不允许使用 unsafe 关键字。我该如何解决这个问题?

unsafe
{
    private byte* ReadIntoMemory(int rowIndex, int colIndex, int rowSize, int colSize, byte *abc)
    {
    }
}

如果我不使用 unsafe,那么编译器会将方法的 byte* 参数标记为无效语法。

我试图将 ReadIntoMemory 方法编码为 return 对正确内存位置的引用,代码如下 -

byte *interestingMemoryLocation = ReadIntoMemory(locX, locY, sizeX, sizeY, basePointer);

您需要将 unsafe 关键字移动到方法签名中:

private unsafe byte* ReadIntoMemory(…