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); }
internal static void SetCollectionProperty(object resource, string propertyName, IEdmCollectionTypeReference edmPropertyType, object value, bool clearCollection) { if (value != null) { IEnumerable collection = value as IEnumerable; Contract.Assert(collection != null, "SetCollectionProperty is always passed the result of ODataFeedDeserializer or ODataCollectionDeserializer"); Type resourceType = resource.GetType(); Type propertyType = GetPropertyType(resource, propertyName); Type elementType; if (!propertyType.IsCollection(out elementType)) { string message = Error.Format(SRResources.PropertyIsNotCollection, propertyType.FullName, propertyName, resourceType.FullName); throw new SerializationException(message); } IEnumerable newCollection; if (CanSetProperty(resource, propertyName) && CollectionDeserializationHelpers.TryCreateInstance(propertyType, edmPropertyType, elementType, out newCollection)) { // settable collections collection.AddToCollection(newCollection, elementType, resourceType, propertyName, propertyType); if (propertyType.IsArray) { newCollection = CollectionDeserializationHelpers.ToArray(newCollection, elementType); } SetProperty(resource, propertyName, newCollection); } else { // get-only collections. newCollection = GetProperty(resource, propertyName) as IEnumerable; if (newCollection == null) { string message = Error.Format(SRResources.CannotAddToNullCollection, propertyName, resourceType.FullName); throw new SerializationException(message); } if (clearCollection) { newCollection.Clear(propertyName, resourceType); } collection.AddToCollection(newCollection, elementType, resourceType, propertyName, propertyType); } } }
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()); }
internal static void SetCollectionProperty(object resource, string propertyName, bool isDelta, object value) { if (value != null) { IEnumerable collection = value as IEnumerable; Contract.Assert(collection != null, "SetCollectionProperty is always passed the result of ODataFeedDeserializer or ODataCollectionDeserializer"); Type resourceType = resource.GetType(); Type propertyType = GetPropertyType(resource, propertyName, isDelta); Type elementType; if (!propertyType.IsCollection(out elementType)) { throw Error.InvalidOperation(SRResources.PropertyIsNotCollection, propertyType.FullName, propertyName, resourceType.FullName); } IEnumerable newCollection; if (CanSetProperty(resource, propertyName, isDelta) && CollectionDeserializationHelpers.TryCreateInstance(propertyType, elementType, out newCollection)) { // settable collections collection.AddToCollection(newCollection, elementType, resourceType, propertyName, propertyType); if (propertyType.IsArray) { newCollection = CollectionDeserializationHelpers.ToArray(newCollection, elementType); } SetProperty(resource, propertyName, isDelta, newCollection); } else { // get-only collections. newCollection = GetProperty(resource, propertyName, isDelta) as IEnumerable; if (newCollection == null) { throw Error.InvalidOperation(SRResources.CannotAddToNullCollection, propertyName, resourceType.FullName); } collection.AddToCollection(newCollection, elementType, resourceType, propertyName, propertyType); } } }