// Generated from executable documentation examples. Edit the source region, then regenerate. using System; using System.IO; using System.IO.Pipelines; using System.Linq; using System.Buffers; using System.Collections.Generic; using System.Dynamic; using System.Globalization; using System.Numerics; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using CStructSharp; using CStructSharp.Codecs; using CStructSharp.Diagnostics; using CStructSharp.Introspection; using CStructSharp.Values; internal static partial class Program { public static void Main() { HeaderRoundTrip(); Console.WriteLine("PASS header-round-trip"); } private static void HeaderRoundTrip() { var layout = new CStruct("struct header { uint16 kind; uint32 length; };"); byte[] bytes = layout.Serialize("header", new Dictionary { ["kind"] = 2, ["length"] = 6 }); SequenceEqual([2, 0, 6, 0, 0, 0], bytes); using var stream = new MemoryStream(bytes); layout.Update(stream, "header.kind", 3); SequenceEqual([3, 0, 6, 0, 0, 0], stream.ToArray()); Header header = layout.ReadValue
(stream.ToArray().AsSpan(), "header"); Equal((ushort)3, header.Kind); Equal(6U, header.Length); Console.WriteLine($"Updated kind = {header.Kind}; length = {header.Length}"); } private static void Equal(T expected, T actual) { if (!EqualityComparer.Default.Equals(expected, actual)) { throw new InvalidOperationException($"Expected '{expected}', received '{actual}'."); } } private static void SequenceEqual(byte[] expected, byte[] actual) { if (!expected.AsSpan().SequenceEqual(actual)) { throw new InvalidOperationException( $"Expected {Convert.ToHexString(expected)}, received {Convert.ToHexString(actual)}."); } } public sealed class Header : ICStructMapped
{ public ushort Kind { get; set; } public uint Length { get; set; } public static Header ReadFrom(StructValue source) { return new Header { Kind = source.Get("kind"), Length = source.Get("length") }; } public static void WriteTo(Header value, StructValue target) { target["kind"] = value.Kind; target["length"] = value.Length; } [ModuleInitializer] internal static void Register() { MappedTypes.Register
(); } } #region api-guide-map-mapped-type // The generator writes ReadFrom, WriteTo, and the registration for this partial class. [CStructMapped] public sealed partial class MappedPoint { public short X { get; set; } public short Y { get; set; } } #endregion }