示例#1
0
        protected static object CreateCollectionInstance(ODataProperty collectionProperty, Type userCollectionType, System.Data.Services.Client.ResponseInfo responseInfo)
        {
            object obj2;
            ODataCollectionValue value2     = collectionProperty.Value as ODataCollectionValue;
            ClientTypeAnnotation annotation = responseInfo.TypeResolver.ResolveEdmTypeName(userCollectionType, value2.TypeName);

            if (IsDataServiceCollection(annotation.ElementType))
            {
                throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.AtomMaterializer_DataServiceCollectionNotSupportedForNonEntities);
            }
            try
            {
                obj2 = annotation.CreateInstance();
            }
            catch (MissingMethodException exception)
            {
                throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.AtomMaterializer_NoParameterlessCtorForCollectionProperty(collectionProperty.Name, annotation.ElementTypeName), exception);
            }
            return(obj2);
        }
示例#2
0
        protected static void ApplyCollectionDataValues(ODataProperty collectionProperty, bool ignoreMissingProperties, System.Data.Services.Client.ResponseInfo responseInfo, object collectionInstance, Type collectionItemType, Action <object, object> AddValueToBackingICollectionInstance)
        {
            ODataCollectionValue value2 = collectionProperty.Value as ODataCollectionValue;

            if (value2.Items != null)
            {
                bool           flag  = PrimitiveType.IsKnownNullableType(collectionItemType);
                ClientEdmModel model = ClientEdmModel.GetModel(responseInfo.MaxProtocolVersion);
                foreach (object obj2 in value2.Items)
                {
                    if (obj2 == null)
                    {
                        throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.Collection_NullCollectionItemsNotSupported);
                    }
                    if (flag)
                    {
                        object obj3;
                        if ((obj2 is ODataComplexValue) || (obj2 is ODataCollectionValue))
                        {
                            throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.Collection_ComplexTypesInCollectionOfPrimitiveTypesNotAllowed);
                        }
                        MaterializePrimitiveDataValue(collectionItemType, value2.TypeName, obj2, responseInfo, () => System.Data.Services.Client.Strings.Collection_NullCollectionItemsNotSupported, out obj3);
                        AddValueToBackingICollectionInstance(collectionInstance, ConvertPrimitiveValue(obj2, collectionItemType));
                    }
                    else
                    {
                        ODataComplexValue value3 = obj2 as ODataComplexValue;
                        if (value3 == null)
                        {
                            throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.Collection_PrimitiveTypesInCollectionOfComplexTypesNotAllowed);
                        }
                        ClientTypeAnnotation clientTypeAnnotation = model.GetClientTypeAnnotation(model.GetOrCreateEdmType(collectionItemType));
                        object instance = clientTypeAnnotation.CreateInstance();
                        ApplyDataValues(clientTypeAnnotation, value3.Properties, ignoreMissingProperties, responseInfo, instance);
                        AddValueToBackingICollectionInstance(collectionInstance, instance);
                    }
                }
            }
            collectionProperty.SetMaterializedValue(collectionInstance);
        }
示例#3
0
        protected static void ApplyDataValue(ClientTypeAnnotation type, ODataProperty property, bool ignoreMissingProperties, System.Data.Services.Client.ResponseInfo responseInfo, object instance)
        {
            ClientPropertyAnnotation annotation = type.GetProperty(property.Name, ignoreMissingProperties);

            if (annotation != null)
            {
                if (annotation.IsPrimitiveOrComplexCollection)
                {
                    if (property.Value == null)
                    {
                        throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.Collection_NullCollectionNotSupported(property.Name));
                    }
                    if (property.Value is string)
                    {
                        throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.Deserialize_MixedTextWithComment);
                    }
                    if (property.Value is ODataComplexValue)
                    {
                        throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.AtomMaterializer_InvalidCollectionItem(property.Name));
                    }
                    object obj2 = annotation.GetValue(instance);
                    if (obj2 == null)
                    {
                        obj2 = CreateCollectionInstance(property, annotation.PropertyType, responseInfo);
                        annotation.SetValue(instance, obj2, property.Name, false);
                    }
                    else
                    {
                        annotation.ClearBackingICollectionInstance(obj2);
                    }
                    ApplyCollectionDataValues(property, ignoreMissingProperties, responseInfo, obj2, annotation.PrimitiveOrComplexCollectionItemType, new Action <object, object>(annotation.AddValueToBackingICollectionInstance));
                }
                else
                {
                    object            obj3   = property.Value;
                    ODataComplexValue value2 = obj3 as ODataComplexValue;
                    if ((obj3 != null) && (value2 != null))
                    {
                        if (!annotation.EdmProperty.Type.IsComplex())
                        {
                            throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.Deserialize_ExpectingSimpleValue);
                        }
                        bool                 flag  = false;
                        ClientEdmModel       model = ClientEdmModel.GetModel(responseInfo.MaxProtocolVersion);
                        ClientTypeAnnotation clientTypeAnnotation = model.GetClientTypeAnnotation(model.GetOrCreateEdmType(annotation.PropertyType));
                        object               obj4 = annotation.GetValue(instance);
                        if (obj4 == null)
                        {
                            obj4 = clientTypeAnnotation.CreateInstance();
                            flag = true;
                        }
                        MaterializeDataValues(clientTypeAnnotation, value2.Properties, ignoreMissingProperties);
                        ApplyDataValues(clientTypeAnnotation, value2.Properties, ignoreMissingProperties, responseInfo, obj4);
                        if (flag)
                        {
                            annotation.SetValue(instance, obj4, property.Name, true);
                        }
                    }
                    else
                    {
                        MaterializePrimitiveDataValue(annotation.NullablePropertyType, property);
                        annotation.SetValue(instance, property.GetMaterializedValue(), property.Name, true);
                    }
                }
            }
        }