コード例 #1
0
        private bool TryDeserializeList(ProtoReader reader, DataFormat format, int tag, Type listType, Type itemType, ref object value)
        {
            bool       isList;
            MethodInfo addMethod = TypeModel.ResolveListAdd(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];
            while (TryDeserializeAuxiliaryType(reader, format, tag, itemType, ref nextItem, true, true))
            {
                found = true;
                if (value == null)
                {
                    Type concreteListType = listType;
                    if (!listType.IsClass || listType.IsAbstract || listType.GetConstructor(
                            BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                            null, Helpers.EmptyTypes, null) == null)
                    {
                        concreteListType = typeof(System.Collections.Generic.List <>).MakeGenericType(itemType);
                    }
                    value = (Activator.CreateInstance(concreteListType));
                    list  = value as IList;
                }
                if (list == null)
                {
                    args[0] = nextItem;
                    addMethod.Invoke(value, args);
                }
                else
                {
                    list.Add(nextItem);
                }
                nextItem = null;
            }
            return(found);
        }