jefffhaynes / BinarySerializer

A declarative serialization framework for controlling formatting of data at the byte and bit level using field bindings, converters, and code.
MIT License
290 stars 62 forks source link

FieldLengthAttribute where length value includes size of field that stores length #228

Open lOlbas opened 10 months ago

lOlbas commented 10 months ago

Consider the following payload:

          00 01 02 03 04 05
00000000  06 00 01 02 03 04

First two bytes indicate the size of the entire payload, not just the data after it. This is the way I would describe this message:

public class RootEvent
{
    [FieldOrder(1)]
    public ushort DataLength;

    [FieldOrder(2)]
    [FieldLength(nameof(DataLength), -sizeof(ushort)] // Second parameter is "adjustment" for the actual length
    public object Data;
}

However I don't seem to find a way to implement something similar using this library, did I miss something? 🤔