void ReadGetOnlyCollection(CollectionDataContract collectionContract)
        {
            Type   type     = collectionContract.UnderlyingType;
            Type   itemType = collectionContract.ItemType;
            bool   isArray  = (collectionContract.Kind == CollectionKind.Array);
            string itemName = collectionContract.ItemName;
            string itemNs   = collectionContract.StableName.Namespace;

            objectLocal = context.GetCollectionMember();

            //check that items are actually going to be deserialized into the collection
            if (IsStartElement(this.itemName, this.itemNamespace))
            {
                if (objectLocal == null)
                {
                    XmlObjectSerializerReadContext.ThrowNullValueReturnedForGetOnlyCollectionException(type);
                }
                else
                {
                    int size = 0;
                    if (isArray)
                    {
                        size = ((Array)objectLocal).Length;
                    }
                    context.AddNewObject(objectLocal);
                    for (int i = 0; i < int.MaxValue;)
                    {
                        if (IsStartElement(this.itemName, this.itemNamespace))
                        {
                            context.IncrementItemCount(1);
                            var value = ReadCollectionItem(collectionContract, itemType, itemName, itemNs);
                            if (isArray)
                            {
                                if (size == i)
                                {
                                    XmlObjectSerializerReadContext.ThrowArrayExceededSizeException(size, type);
                                }
                                else
                                {
                                    ((Array)objectLocal).SetValue(value, i);
                                }
                            }
                            else
                            {
                                StoreCollectionValue(objectLocal, itemType, value, collectionContract);
                            }
                        }
                        else if (IsEndElement())
                        {
                            break;
                        }
                        else
                        {
                            HandleUnexpectedItemInCollection(ref i);
                        }
                    }
                    context.CheckEndOfArray(xmlReader, size, this.itemName, this.itemNamespace);
                }
            }
        }
Пример #2
0
        public void ReflectionReadGetOnlyCollection(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, XmlDictionaryString collectionItemName, XmlDictionaryString collectionItemNamespace, CollectionDataContract collectionContract)
        {
            object resultCollection = context.GetCollectionMember();

            if (ReflectionReadSpecialCollection(xmlReader, context, collectionContract, resultCollection))
            {
                return;
            }

            if (xmlReader.IsStartElement(collectionItemName, collectionItemNamespace))
            {
                if (resultCollection == null)
                {
                    XmlObjectSerializerReadContext.ThrowNullValueReturnedForGetOnlyCollectionException(collectionContract.UnderlyingType);
                }

                bool isReadOnlyCollection = true;
                resultCollection = ReadCollectionItems(xmlReader, context, collectionItemName, collectionItemNamespace, collectionContract, resultCollection, isReadOnlyCollection);
            }
        }