I want to convert a byte* to a byte[], but I also want to have a reusable function to do this:
public unsafe static T[] Create
I have no idea whatsoever if the following would work, but it might (at least it compiles :):
public unsafe static T[] Create(void* ptr, int length) where T : struct
{
T[] array = new T[length];
for (int i = 0; i < length; i++)
{
array[i] = (T)Marshal.PtrToStructure(new IntPtr(ptr), typeof(T));
}
return array;
}
The key is to use Marshal.PtrToStructure to convert to the correct type.