不安代码只会在使用/unsafe编译的情况下出现,这个异常怎么解决?
如下代码在C#中用到了指针: unsafe public static PhoneAddress GetPhoneNumber()
{
PhoneAddress phoneaddr = new PhoneAddress();
Byte[] buffer = new Byte[516];
fixed (byte* pAddr = buffer) {
IntPtr res = SmsGetPhoneNumber((IntPtr)pAddr);
if (res != IntPtr.Zero)
throw new Exception("Could not get phone number from SIM");
byte* pCurrent = pAddr;
phoneaddr.AddressType = (AddressType)Marshal.ReadInt32((IntPtr)pCurrent);
pCurrent += Marshal.SizeOf(phoneaddr.AddressType);
phoneaddr.Address = Marshal.PtrToStringUni((IntPtr)pCurrent);
}
return phoneaddr;
}