Frequently in my integration work, I have a native memory buffer either accessed or created by a 3rd party library. When passing it to the managed world, frequently as a byte to be worked on by Stream.Read() or Write(), I am forced to create a temporary arrays and do a memory copy, hence a big performance loss. I would like to be able to take a native ptr and create a byte out of it, so it can be passed around into the managed function.
/// Reads up to nBuf bytes from the stream s into the buffer at pBuf.
unsafe int ReadData(byte* pBuf, int nBuf, Stream s)
// Create a wrapper byte object that points to the native pBuf memory
// The <T> type should have the same restrictions as in fixed() operator
byte buf = Array.CreateNative<byte>(pBuf, nBuf);
// Use the wrapper as a managed array object
return s.Read(buf, 0, nBuf);