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
289 stars 62 forks source link

Can't deserialize array of class #163

Closed sn4k3 closed 1 year ago

sn4k3 commented 3 years ago
        public class LayerDefinition
        {
            public const string SectionMark = "LAYERDEF";

            [FieldOrder(0)] public Section Section { get; set; }

            [FieldOrder(1)] public uint LayersCount { get; set; }

            [FieldOrder(2)] [FieldCount(nameof(LayersCount))] public LayerData[] Layers { get; set; } // Problem here
        }

        public class LayerData
        {
            public const byte ClassSize = 32;
            /// <summary>
            /// Gets the layer image offset to encoded layer data, and its length in bytes.
            /// </summary>
            [FieldOrder(0)] public uint DataAddress { get; set; }

            /// <summary>
            /// Gets the layer image length in bytes.
            /// </summary>
            [FieldOrder(1)] public uint DataLength { get; set; }

            [FieldOrder(2)] public float LiftHeight { get; set; }

            [FieldOrder(3)] public float LiftSpeed { get; set; }

            /// <summary>
            /// Gets the exposure time for this layer, in seconds.
            /// </summary>
            [FieldOrder(4)]
            public float LayerExposure { get; set; }

            /// <summary>
            /// Gets the build platform Z position for this layer, measured in millimeters.
            /// </summary>
            [FieldOrder(5)]
            public float LayerPositionZ { get; set; }

            [FieldOrder(6)] public float Offset1 { get; set; }
            [FieldOrder(7)] public float Offset2 { get; set; }

            [Ignore] public byte[] EncodedRle { get; set; }
            [Ignore] public PWSFile Parent { get; set; }

            public LayerData()
            {
            }
       }

With class LayerDefinition on deserialize, the array LayerData comes with correct size (LayersCount) but all entries are nulled. Does it require a List instead of array?

jefffhaynes commented 3 years ago

This is working for me. Maybe you can post some more code to help reproduce the problem.

public void Roundtrip()
{
    var expected = new LayerDefinition
    {
        Layers = new List<LayerData>
        {
            new LayerData(),
            new LayerData()
        }.ToArray()
    };

    var actual = Roundtrip(expected);
    Assert.AreEqual(expected.Layers.Length, actual.Layers.Length);
    Assert.IsNotNull(actual.Layers[0]);
    Assert.IsNotNull(actual.Layers[1]);
}
jefffhaynes commented 1 year ago

Please reopen if this is still an issue.