public void TryCreateInstance_Creates_AppropriateCollectionObject(Type collectionType, Type elementType)
        {
            IEnumerable result;
            bool        created = CollectionDeserializationHelpers.TryCreateInstance(collectionType, null, elementType, out result);

            Assert.True(created);
            Assert.IsAssignableFrom(collectionType, result);
        }
Esempio n. 2
0
        private static object ConvertCollection(ODataCollectionValue collectionValue,
                                                IEdmTypeReference edmTypeReference, Type clrType, string parameterName,
                                                ODataDeserializerContext readContext, IServiceProvider requestContainer)
        {
            Contract.Assert(collectionValue != null);

            IEdmCollectionTypeReference collectionType = edmTypeReference as IEdmCollectionTypeReference;

            Contract.Assert(collectionType != null);

            ODataDeserializerProvider deserializerProvider =
                requestContainer.GetRequiredService <ODataDeserializerProvider>();
            ODataCollectionDeserializer deserializer =
                (ODataCollectionDeserializer)deserializerProvider.GetEdmTypeDeserializer(collectionType);

            object value = deserializer.ReadInline(collectionValue, collectionType, readContext);

            if (value == null)
            {
                return(null);
            }

            IEnumerable collection = value as IEnumerable;

            Contract.Assert(collection != null);

            Type elementType;

            if (!clrType.IsCollection(out elementType))
            {
                // EdmEntityCollectionObject and EdmComplexCollectionObject are collection types.
                throw new ODataException(String.Format(CultureInfo.InvariantCulture,
                                                       SRResources.ParameterTypeIsNotCollection, parameterName, clrType));
            }

            IEnumerable newCollection;

            if (CollectionDeserializationHelpers.TryCreateInstance(clrType, collectionType, elementType,
                                                                   out newCollection))
            {
                collection.AddToCollection(newCollection, elementType, parameterName, clrType);
                if (clrType.IsArray)
                {
                    newCollection = CollectionDeserializationHelpers.ToArray(newCollection, elementType);
                }

                return(newCollection);
            }

            return(null);
        }
        public void TryCreateInstance_EdmEntityObjectCollection_SetsEdmType()
        {
            EdmEntityType entityType = new EdmEntityType("NS", "EntityType");
            IEdmCollectionTypeReference entityCollectionType =
                new EdmCollectionType(entityType.ToEdmTypeReference(true))
                .ToEdmTypeReference(true).AsCollection();

            IEnumerable result;

            CollectionDeserializationHelpers.TryCreateInstance(typeof(EdmEntityObjectCollection), entityCollectionType, typeof(EdmComplexObject), out result);

            var edmObject = Assert.IsType <EdmEntityObjectCollection>(result);

            Assert.Equal(edmObject.GetEdmType(), entityCollectionType, new EdmTypeReferenceEqualityComparer());
        }
Esempio n. 4
0
        internal static void SetCollectionProperty(object resource, string propertyName, IEdmCollectionTypeReference edmPropertyType, object value, bool clearCollection)
        {
            if (value == null)
            {
                return;
            }
            IEnumerable items        = value as IEnumerable;
            Type        type         = resource.GetType();
            Type        propertyType = DeserializationHelpers.GetPropertyType(resource, propertyName);
            Type        elementType;

            if (!propertyType.IsCollection(out elementType))
            {
                throw new SerializationException(System.Web.Http.Error.Format(SRResources.PropertyIsNotCollection, (object)propertyType.FullName, (object)propertyName, (object)type.FullName));
            }
            IEnumerable instance;

            if (DeserializationHelpers.CanSetProperty(resource, propertyName) && CollectionDeserializationHelpers.TryCreateInstance(propertyType, edmPropertyType, elementType, out instance))
            {
                items.AddToCollection(instance, elementType, type, propertyName, propertyType);
                if (propertyType.IsArray)
                {
                    instance = CollectionDeserializationHelpers.ToArray(instance, elementType);
                }
                DeserializationHelpers.SetProperty(resource, propertyName, (object)instance);
            }
            else
            {
                IEnumerable property = DeserializationHelpers.GetProperty(resource, propertyName) as IEnumerable;
                if (property == null)
                {
                    throw new SerializationException(System.Web.Http.Error.Format(SRResources.CannotAddToNullCollection, (object)propertyName, (object)type.FullName));
                }
                if (clearCollection)
                {
                    property.Clear(propertyName, type);
                }
                items.AddToCollection(property, elementType, type, propertyName, propertyType);
            }
        }