/// <summary> /// Reinterprets the provided <see cref="bytes"/> to the specified generic array of value types. /// </summary> /// <typeparam name="TConvertType">The element type to reinterpret to.</typeparam> /// <param name="bytes">The bytes chunk.</param> /// <returns>The array of converted values.</returns> public static TConvertType[] ReinterpretToArray <TConvertType>(this Span <byte> bytes) where TConvertType : unmanaged { //Don't check nullness for perf. Callers shouldn't give us null arrays if (bytes.Length == 0) { return(Array.Empty <TConvertType>()); } if (bytes.Length % MarshalSizeOf <TConvertType> .SizeOf != 0) { ThrowHelpers.ThrowMismatchedArraySizeForElementType <char>(); } return(ReinterpretPrimitiveArray <TConvertType>(bytes)); }
/// <summary> /// Reinterprets the provided <see cref="bytes"/> to the specified generic array of value types. /// </summary> /// <typeparam name="TConvertType">The element type to reinterpret to.</typeparam> /// <param name="bytes">The bytes chunk.</param> /// <returns>The array of converted values.</returns> public static unsafe TConvertType[] ReinterpretToArray <TConvertType>(this byte[] bytes) where TConvertType : struct { //Don't check nullness for perf. Callers shouldn't give us null arrays if (bytes.Length == 0) { return(new TConvertType[0]); } if (!TypeIntrospector <TConvertType> .IsPrimitive) { ThrowHelpers.ThrowOnlyPrimitivesException <TConvertType>(); } if (bytes.Length % MarshalSizeOf <TConvertType> .SizeOf != 0) { ThrowHelpers.ThrowMismatchedArraySizeForElementType <char>(); } return(ReinterpretPrimitiveArray <TConvertType>(bytes)); }