Пример #1
0
        private bool TryDeserializeList(TypeModel model, ProtoReader reader, DataFormat format, int tag, Type listType, Type itemType, ref object value)
        {
            bool       isList;
            MethodInfo addMethod = TypeModel.ResolveListAdd(model, listType, itemType, out isList);

            if (addMethod == null)
            {
                throw new NotSupportedException("Unknown list variant: " + listType.FullName);
            }
            bool   found    = false;
            object nextItem = null;
            IList  list     = value as IList;

            object[]  args           = isList ? null : new object[1];
            BasicList arraySurrogate = listType.IsArray ? new BasicList() : null;

            while (this.TryDeserializeAuxiliaryType(reader, format, tag, itemType, ref nextItem, true, true, true, true))
            {
                found = true;
                if (value == null && arraySurrogate == null)
                {
                    value = TypeModel.CreateListInstance(listType, itemType);
                    list  = (value as IList);
                }
                if (list != null)
                {
                    list.Add(nextItem);
                }
                else
                {
                    if (arraySurrogate != null)
                    {
                        arraySurrogate.Add(nextItem);
                    }
                    else
                    {
                        args[0] = nextItem;
                        addMethod.Invoke(value, args);
                    }
                }
                nextItem = null;
            }
            if (arraySurrogate != null)
            {
                if (value != null)
                {
                    if (arraySurrogate.Count != 0)
                    {
                        Array existing = (Array)value;
                        Array newArray = Array.CreateInstance(itemType, existing.Length + arraySurrogate.Count);
                        Array.Copy(existing, newArray, existing.Length);
                        arraySurrogate.CopyTo(newArray, existing.Length);
                        value = newArray;
                    }
                }
                else
                {
                    Array newArray = Array.CreateInstance(itemType, arraySurrogate.Count);
                    arraySurrogate.CopyTo(newArray, 0);
                    value = newArray;
                }
            }
            return(found);
        }