Exemplo n.º 1
0
            /// <summary>
            /// Visits a collection item.
            /// </summary>
            /// <param name="collectionValue">The collection to visit.</param>
            protected override ODataPayloadElement VisitCollectionValue(ODataCollectionValue collectionValue)
            {
                if (collectionValue == null)
                {
                    return(new PrimitiveMultiValue(null, true));
                }

                bool?isPrimitiveCollection = null;

                // Try to parse the type name and if the item type name is a primitive EDM type, then this is a primitive collection.
                string typeName = collectionValue.TypeName;

                if (typeName != null)
                {
                    string itemTypeName = EntityModelUtils.GetCollectionItemTypeName(typeName);
                    isPrimitiveCollection = itemTypeName != null && EntityModelUtils.GetPrimitiveEdmType(itemTypeName) != null;
                }

                List <object> items = new List <object>();

                foreach (object item in collectionValue.Items)
                {
                    if (!isPrimitiveCollection.HasValue)
                    {
                        ODataComplexValue complexItemValue = item as ODataComplexValue;

                        // If the first item is a complex value, then the collection is of complex kind.
                        // Note that if the first item is null, we assume primitive collection, since we can't really decide (and it's an invalid thing anyway)
                        isPrimitiveCollection = complexItemValue == null;
                    }

                    if (isPrimitiveCollection.Value)
                    {
                        items.Add((PrimitiveValue)this.Visit(item));
                    }
                    else
                    {
                        ODataComplexValue complexItemValue = item as ODataComplexValue;
                        ExceptionUtilities.Assert(
                            item == null || complexItemValue != null,
                            "The collection was determined to be of complex values but one of its items is not an ODataComplexValue.");
                        items.Add((ComplexInstance)this.Visit(complexItemValue));
                    }
                }

                if (!isPrimitiveCollection.HasValue)
                {
                    // If we could not tell until now (possible only if there was no type name and no items)
                    // assume primitive collection.
                    isPrimitiveCollection = true;
                }

                if (isPrimitiveCollection == true)
                {
                    PrimitiveMultiValue primitiveCollection = new PrimitiveMultiValue(typeName, false);
                    foreach (object item in items)
                    {
                        primitiveCollection.Add((PrimitiveValue)item);
                    }

                    this.ConvertSerializationTypeNameAnnotation(collectionValue, primitiveCollection);

                    return(primitiveCollection);
                }
                else
                {
                    ComplexMultiValue complexCollection = new ComplexMultiValue(typeName, false);
                    foreach (object item in items)
                    {
                        complexCollection.Add((ComplexInstance)item);
                    }

                    this.ConvertSerializationTypeNameAnnotation(collectionValue, complexCollection);

                    return(complexCollection);
                }
            }