3

我可以var b = *myObject;在即时窗口中进行操作,它给了我一个0x123456格式化的值。但我不能在代码中做到这一点。然后它说

* 或 -> 运算符必须应用于指针

为什么我可以在即时窗口中执行此操作,但不能在代码中执行此操作?

4

1 回答 1

0

使用不安全指针与封送处理的示例访问

IntPtr pLineBuf;
int[] lineData;
//.
//.
//.

pLineBuf = Marshal.AllocHGlobal(8192 * sizeof(byte));
lineData = new int[8192];

// access elements like this
unsafe
{
    byte* pa = (byte*)plineBuf;  // cast from IntPtr
    for (int j=0; j<10; j++)
       lineData[j] = *(pa + j);    
}

// OR

for (int j=0; j<10; j++)
     lineData[j] = (Marshal.ReadByte(plineBuf + j)); 
于 2018-08-06T13:36:08.470 回答