/// <summary>
        /// Writes the raw data of the supplied struct to this stream asynchronously.
        /// </summary>
        /// <typeparam name="T">The type of the struct.</typeparam>
        /// <param name="stream">The stream that should be written to.</param>
        /// <param name="struct">The struct that should be written to the stream.</param>
        /// <returns>A task that represents the asynchronous write operation.</returns>
        public static async Task WriteRawDataAsync <T>(this Stream stream, T @struct)
            where T : struct
        {
            int size = Marshal.SizeOf(typeof(T));

            byte[] data = RawSerializer.GetRawDataInternal <T>(@struct, size);
            await stream.WriteAsync(data, 0, size);
        }
Esempio n. 2
0
        byte[] GetRawData <T>(
#if USE_EXTENSIONS
            this
#endif
            T @struct)
            where T : struct
        {
            return(RawSerializer.GetRawDataInternal <T>(@struct, Marshal.SizeOf(typeof(T))));
        }
        /// <summary>
        /// Writes the raw data of the supplied struct to this stream asynchronously and monitors cancellation requests.
        /// </summary>
        /// <typeparam name="T">The type of the struct.</typeparam>
        /// <param name="stream">The stream that should be written to.</param>
        /// <param name="struct">The struct that should be written to the stream.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is System.Threading.CancellationToken.None.</param>
        /// <returns>A task that represents the asynchronous write operation.</returns>
        public static async Task WriteRawDataAsync <T>(this Stream stream, T @struct, CancellationToken cancellationToken)
            where T : struct
        {
            cancellationToken.ThrowIfCancellationRequested();
            int size = Marshal.SizeOf(typeof(T));

            byte[] data = RawSerializer.GetRawDataInternal <T>(@struct, size);
            await stream.WriteAsync(data, 0, size, cancellationToken);
        }
Esempio n. 4
0
        /// <summary>
        /// Writes the raw data of the supplied struct to this BinaryWriter.
        /// </summary>
        /// <typeparam name="T">The type of the struct.</typeparam>
        /// <param name="writer">The BinaryWriter that should be written to</param>
        /// <param name="struct">The struct that should be written to the BinaryWriter.</param>
        public static void WriteRawData <T>(
#if USE_EXTENSIONS
            this
#endif
            BinaryWriter writer, T @struct)
            where T : struct
        {
            int size = Marshal.SizeOf(typeof(T));

            byte[] data = RawSerializer.GetRawDataInternal <T>(@struct, size);
            writer.Write(data, 0, size);
        }