[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static TOut Reinterpret<TIn, TOut>(TIn curValue, int sizeBytes)
where TIn : struct
where TOut : struct
{
TOut result = default(TOut);
TypedReference resultRef = __makeref(result);
byte* resultPtr = (byte*) *((IntPtr*) &resultRef);
TypedReference curValueRef = __makeref(curValue);
byte* curValuePtr = (byte*) *((IntPtr*) &curValueRef);
for (int i = 0; i < sizeBytes; ++i) {
resultPtr[i] = curValuePtr[i];
}
return result;
}
Code snippet taken from "Fun With __makeref".