コード例 #1
0
ファイル: Program.cs プロジェクト: ishkang/Gorgon
        /// <summary>
        /// Function to write the contents pointed at by an unsafe pointer to a stream using the GorgonBinaryWriter and reading it back again using the GorgonBinaryReader.
        /// </summary>
        /// <param name="stream">The stream that will receive the data.</param>
        private static unsafe void WritePointer(MemoryStream stream)
        {
            stream.Position = 0;
            var writer = new GorgonBinaryWriter(stream, true);
            var reader = new GorgonBinaryReader(stream, true);

            try
            {
                var data = new SomeTestData
                {
                    Value1 = 1,
                    Value2 = 2.1,
                    Value3 = 3
                };

                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("Writing/Reading pointer to memory into a memory stream.");
                Console.ForegroundColor = ConsoleColor.White;

                writer.Write(&data, Unsafe.SizeOf <SomeTestData>());

                stream.Position = 0;

                SomeTestData readData = default;
                reader.Read(&readData, Unsafe.SizeOf <SomeTestData>());

                Console.WriteLine($"int32 Value1 = 1: {readData.Value1 == 1}");
                Console.WriteLine($"double Value2 = 2.1: {readData.Value2.EqualsEpsilon(2.1)}");
                Console.WriteLine($"int16 Value3 = 3: {readData.Value3 == 3}");

                stream.Position = 0;
            }
            finally
            {
                writer.Dispose();
                reader.Dispose();
            }
        }