public override void SetValue(TEntityType entity, object value) { if ((object)entity == null) { throw new ArgumentNullException(nameof(entity)); } if (this._isCollection) { DeserializationHelpers.SetCollectionProperty((object)entity, this._property.Name, (IEdmCollectionTypeReference)null, value, true); } else { this._setter(entity, value); } }
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); } }
internal static void ApplyProperty(ODataProperty property, IEdmStructuredTypeReference resourceType, object resource, ODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext) { IEdmProperty property1 = resourceType.FindProperty(property.Name); string name = property.Name; IEdmTypeReference type = property1?.Type; EdmTypeKind typeKind; object obj = DeserializationHelpers.ConvertValue(property.Value, ref type, deserializerProvider, readContext, out typeKind); switch (typeKind) { case EdmTypeKind.Primitive: if (!readContext.IsUntyped) { obj = EdmPrimitiveHelpers.ConvertPrimitiveValue(obj, DeserializationHelpers.GetPropertyType(resource, name)); break; } break; case EdmTypeKind.Collection: DeserializationHelpers.SetCollectionProperty(resource, property1, obj); return; } DeserializationHelpers.SetProperty(resource, name, obj); }
internal static object ConvertValue(object oDataValue, ref IEdmTypeReference propertyType, ODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext, out EdmTypeKind typeKind) { if (oDataValue == null) { typeKind = EdmTypeKind.None; return((object)null); } ODataComplexValue complexValue = oDataValue as ODataComplexValue; if (complexValue != null) { typeKind = EdmTypeKind.Complex; return(DeserializationHelpers.ConvertComplexValue(complexValue, ref propertyType, deserializerProvider, readContext)); } ODataCollectionValue collection = oDataValue as ODataCollectionValue; if (collection != null) { typeKind = EdmTypeKind.Collection; return(DeserializationHelpers.ConvertCollectionValue(collection, propertyType, deserializerProvider, readContext)); } typeKind = EdmTypeKind.Primitive; return(oDataValue); }
internal static void SetCollectionProperty(object resource, IEdmProperty edmProperty, object value) { DeserializationHelpers.SetCollectionProperty(resource, edmProperty.Name, edmProperty.Type.AsCollection(), value, false); }