The unmanaged keyword added to C# 7.3 is designed largely for interop. But it is also said that this might facilitate generic serialization methods. Similarly, this answer shows part of how this might be done -- in particular, to deserialize an array of unmanaged elements.
I am wondering whether this is safe to do. There are at least two concerns. First, it seems based on my experimentation that the sizeof(T) operator still must be used in unsafe context, even where the T : unmanaged constraint applies. This seems consistent with the proposition that even with the unmanaged constraint, different platforms might lay out unmanaged structs differently (unless, for example, LayoutKind.Sequential is used), so sizeof is unreliable for serialization purposes, as was the case before. And, of course, we must use unsafe context to get the bytes underlying the generic T.
Second, numeric types may also have different layouts on different platforms. Microsoft's code using Span thus includes methods that will specifically read / write with a specified endianness, such as here. If we serialize an unmanaged T on one platform and deserialize on another, then it would seem that there is a risk that we will get back numbers with different endianness.
My conclusion, then, is that simply serializing the underlying bytes of T is not safe, unless one has some assurance that the deserialization will take place on the same platform. Is that correct?
Assuming it is, the related question is how we might still serialize an unmanaged T generically. Presumably, we could use reflection on T to figure out which types are used, and we can then determine the layout of T using unsafe code. Then, taking into account whether the current system is little endian or big endian, it should be possible to craft an algorithm that will serialize and deserialize a generic unmanaged T. Is that correct? (Of course, if someone has done this, that would be ideal.)