コード例 #1
0
        void ReadGetOnlyCollection(CollectionDataContract collectionContract)
        {
            Type type     = collectionContract.UnderlyingType;
            Type itemType = collectionContract.ItemType;
            bool isArray  = (collectionContract.Kind == CollectionKind.Array);
            int  size     = 0;

            objectLocal = context.GetCollectionMember();

            bool canReadSimpleDictionary =
                collectionContract.Kind == CollectionKind.Dictionary ||
                collectionContract.Kind == CollectionKind.GenericDictionary;

            bool readSimple = canReadSimpleDictionary && context.UseSimpleDictionaryFormat;

            if (readSimple)
            {
                if (objectLocal == null)
                {
                    XmlObjectSerializerReadContext.ThrowNullValueReturnedForGetOnlyCollectionException(type);
                }
                else
                {
                    ReadSimpleDictionary(collectionContract, itemType);
                    context.CheckEndOfArray(xmlReader, size, this.itemName, emptyDictionaryString);
                }
            }
            else
            {
                //check that items are actually going to be deserialized into the collection
                if (IsStartElement(this.itemName, this.emptyDictionaryString))
                {
                    if (objectLocal == null)
                    {
                        XmlObjectSerializerReadContext.ThrowNullValueReturnedForGetOnlyCollectionException(type);
                    }
                    else
                    {
                        size = 0;
                        if (isArray)
                        {
                            size = ((Array)objectLocal).Length;
                        }
                        for (int i = 0; i < int.MaxValue;)
                        {
                            if (IsStartElement(this.itemName, this.emptyDictionaryString))
                            {
                                context.IncrementItemCount(1);
                                var value = ReadCollectionItem(collectionContract, itemType);
                                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.emptyDictionaryString);
                    }
                }
            }
        }