Example #1
0
        /** <inheritDoc /> */
        public IBinaryObject Build()
        {
            // Assume that resulting length will be no less than header + [fields_cnt] * 12;
            int estimatedCapacity = BinaryObjectHeader.Size + (_vals == null ? 0 : _vals.Count * 12);

            using (var outStream = new BinaryHeapStream(estimatedCapacity))
            {
                BinaryWriter writer = _binary.Marshaller.StartMarshal(outStream);

                writer.SetBuilder(this);

                // All related builders will work in this context with this writer.
                _parent._ctx = new Context(writer);

                try
                {
                    // Write.
                    writer.Write(this);

                    // Process metadata.
                    _binary.Marshaller.FinishMarshal(writer);

                    // Create binary object once metadata is processed.
                    return(new BinaryObject(_binary.Marshaller, outStream.InternalArray, 0,
                                            BinaryObjectHeader.Read(outStream, 0)));
                }
                finally
                {
                    // Cleanup.
                    _parent._ctx.Closed = true;
                }
            }
        }