Exemplo n.º 1
0
        void EmitWriteContent(SerializerCodeGen g, Local enumerable, Operand fieldNumber, bool pack, Action first = null)
        {
            Type listType = g.ctx.MapType(typeof(IList <>)).MakeGenericType(_itemType);

            if (_skipIList || (!enumerable.Type.IsArray && !Helpers.IsAssignableFrom(listType, enumerable.Type)))
            {
                EmitWriteContentEnumerable(g, enumerable, fieldNumber, pack, first);
                return;
            }
            using (var list = g.ctx.Local(listType))
                using (var len = g.ctx.Local(typeof(int)))
                    using (var i = g.ctx.Local(typeof(int)))
                    {
                        g.Assign(list, enumerable.AsOperand.Cast(listType));
                        g.Assign(len, list.AsOperand.Property("Count"));
                        g.If(len.AsOperand > 0);
                        {
                            first?.Invoke();

                            g.For(i.AsOperand.Assign(0), i.AsOperand < len.AsOperand, i.AsOperand.Increment());
                            {
                                EmitWriteElement(g, list.AsOperand[i], fieldNumber, pack, i.AsOperand == 0);
                            }
                            g.End();
                        }
                        g.End();
                        if (enumerable.Type.IsValueType)
                        {
                            g.Assign(enumerable, list);
                        }
                    }
        }