If ptr
is your unsafe pointer, and the array has length len
, you can use Marshal.Copy
like this:
byte[] arr = new byte[len];
Marshal.Copy((IntPtr)ptr, arr, 0, len);
But I do wonder how you came by an unsafe pointer to native memory. Do you really need unsafe here, or can you solve the problem by using IntPtr
instead of an unsafe pointer? And if so then there's probably no need for unsafe code at all.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…