Example #1
0
        public static void WriteStreaming <TWriter, TValue>(
            TWriter writer,
            IEnumerable <TValue> enumerable)
            where TWriter : InternalWriterBase
        {
            Debug.Assert(writer != null);

            if (enumerable == null)
            {
                writer.WriteInt32(-1);
                return;
            }

            var arr = enumerable as TValue[];

            if (arr != null)
            {
                WriterAgentGenerator <TWriter, TValue[]> .Run(writer, arr);

                return;
            }

#if NET45 || NETFX_CORE
            var readonlyCollection = enumerable as IReadOnlyCollection <TValue>;
            if (readonlyCollection != null)
            {
                WriteStreamingAsCounted(writer, enumerable, readonlyCollection.Count);
                return;
            }
#endif
            var collection = enumerable as ICollection <TValue>;
            if (collection != null)
            {
                WriteStreamingAsCounted(writer, enumerable, collection.Count);
                return;
            }

            var collection2 = enumerable as ICollection;
            if (collection2 != null)
            {
                WriteStreamingAsCounted(writer, enumerable, collection2.Count);
                return;
            }

            var run = WriterAgentGenerator <TWriter, TValue> .Run;

            writer.WriteInt32(-2);

            foreach (var value in enumerable)
            {
                writer.WriteBoolean(true);
                run(writer, value);
            }

            writer.WriteBoolean(false);
        }
Example #2
0
        public static Action <TWriter, TValue> CreateWriterAgent <TWriter, TValue>()
        {
            var writerParameter   = Expression.Parameter(typeof(TWriter), "w");
            var instanceParameter = Expression.Parameter(typeof(TValue), "v");

            var agentBodyExpression = WriterAgentGenerator.CreateWriteValueExpression(
                writerParameter,
                instanceParameter);

            var expression = Expression.Lambda <Action <TWriter, TValue> >(
                agentBodyExpression,
                writerParameter,
                instanceParameter);

            return(expression.Compile());
        }