/// <summary>
        /// Serializes and STDF record object to the output stream.
        /// </summary>
        /// <param name="stream">Stream object to serialized the STDF data to.</param>
        /// <param name="obj">STDF record object to serialize.</param>
        public override void Serialize(Stream stream, object obj)
        {
            Buffer.SetLength(0);
            SerializeStream = Buffer;
            EndOfStream     = false;
            EndOfRecord     = false;
            ushort recordLength = 0;

            if (obj is ISTDFRecord record)
            {
                ISurrogate typeSurrogate = TypeSurrogateSelector.GetSurrogate(record.GetType());
                if (typeSurrogate == null)
                {
                    return;
                }
                //WriteHeader(0, record.RecordType);
                //                long recordStartPosition = SerializeStream.Position;
                Type recordType        = record.GetType();
                SerializationInfo info = SerializationInfo.Create(recordType, Converter);
                typeSurrogate.GetObjectData(record, info);
                foreach (SerializationInfoEntry field in info)
                {
                    if (field.ItemCountIndex >= 0)
                    {
                        // field has an item count property, so we are serializing an array.  Get the number of items
                        // to serialize from the item count field serialized earlier (we always serialize according
                        // to the item count field rather than the array size for the array field.  Normally these are
                        // equal but it is not mandatory that they be equal).
                        int itemCount = field.ItemCountIndex != null?info.GetValue <int>((int)field.ItemCountIndex) : 0;

                        if (itemCount > 0)
                        {
                            WriteArray(field.Value, field.Type.GetElementType(), 0, itemCount);
                        }
                    }
                    else
                    {
                        WriteMember(field.Value, field.Type);
                    }
                    // For optional fields, save the position after the last field written
                    // that does not have a missing value.  Per spec, we can truncate any contiguous missing
                    // value fields from the end of the record.
                    if (!field.IsMissingValue)
                    {
                        recordLength = (ushort)(SerializeStream.Position);
                    }
                }
                //recordLength = (ushort)(lastValidPosition - recordStartPosition);
                if (recordLength != record.RecordLength)
                {
                    throw new Exception("Mismatched record length.");
                }
                record.RecordLength = recordLength;
                //SerializeStream.Seek(-(SerializeStream.Position - recordStartPosition + 4), SeekOrigin.Current);
                SerializeStream.Flush();
                SerializeStream = stream;
                WriteHeader(record.RecordLength, record.RecordType);
                //                SerializeStream.Seek(record.RecordLength, SeekOrigin.Current);
                //                SerializeStream.SetLength(SerializeStream.Position);
                Buffer.SetLength(recordLength);
                Buffer.WriteTo(stream);
                EndOfRecord = true;
            }
        }