RSCG – GenPack

RSCG – GenPack
 
 

name GenPack
nuget https://www.nuget.org/packages/GenPack/
link https://github.com/dimohy/GenPack
author dimohy

Generating Binary Serialization and properties for class

 

This is how you can use GenPack .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
	<ItemGroup>
		<PackageReference Include="GenPack" Version=" 0.9.0-preview1" OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
	</ItemGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
</Project>


The code that you will use is


using SerializerDemo;

var p= new Person() { Name= "Andrei Ignat" };
var bytes= p.ToPacket();
var entity = Person.FromPacket(bytes);
Console.WriteLine("name is "+entity.Name);



using GenPack;

namespace SerializerDemo;


[GenPackable]
public partial record Person
{

    public readonly static PacketSchema Schema = PacketSchemaBuilder.Create()
           .@short("Id", "Age description")
           .@string("Name", "Name description")
           .Build();
}



 

The code that is generated is

#pragma warning disable CS0219
namespace SerializerDemo
{
    public partial record Person : GenPack.IGenPackable
    {
        /// <summary>
        /// Age description
        /// </summary>
        public short Id { get; set; }
        /// <summary>
        /// Name description
        /// </summary>
        public string Name { get; set; } = string.Empty;
        public byte[] ToPacket()
        {
            using var ms = new System.IO.MemoryStream();
            ToPacket(ms);
            return ms.ToArray();
        }
        public void ToPacket(System.IO.Stream stream)
        {
            System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);
            writer.Write(Id);
            writer.Write(Name);
        }
        public static SerializerDemo.Person FromPacket(byte[] data)
        {
            using var ms = new System.IO.MemoryStream(data);
            return FromPacket(ms);
        }
        public static SerializerDemo.Person FromPacket(System.IO.Stream stream)
        {
            SerializerDemo.Person result = new SerializerDemo.Person();
            System.IO.BinaryReader reader = new System.IO.BinaryReader(stream);
            int size = 0;
            byte[] buffer = null;
            result.Id = reader.ReadInt16();
            result.Name = reader.ReadString();
            return result;
        }
    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/GenPack