private IEnumerable <ODataProperty> CreatePropertyBag(object graph, ODataSerializerContext writeContext)
        {
            IEnumerable <IEdmStructuralProperty> selectProperties;

            if (writeContext.CurrentProjectionNode != null && writeContext.CurrentProjectionNode.Selects.Any())
            {
                selectProperties = _edmEntityTypeReference
                                   .StructuralProperties()
                                   .Where(p => writeContext.CurrentProjectionNode.Selects.Any(node => node.Name == p.Name));
            }
            else
            {
                selectProperties = _edmEntityTypeReference.StructuralProperties();
            }

            List <ODataProperty> properties = new List <ODataProperty>();

            foreach (IEdmStructuralProperty property in selectProperties)
            {
                ODataSerializer serializer = SerializerProvider.GetEdmTypeSerializer(property.Type);
                if (serializer == null)
                {
                    throw Error.NotSupported(SRResources.TypeCannotBeSerialized, property.Type.FullName(), typeof(ODataMediaTypeFormatter).Name);
                }

                object propertyValue = graph.GetType().GetProperty(property.Name).GetValue(graph, index: null);

                properties.Add(serializer.CreateProperty(propertyValue, property.Name, writeContext));
            }

            return(properties);
        }
        public override ODataProperty CreateProperty(object graph, string elementName, ODataSerializerWriteContext writeContext)
        {
            if (String.IsNullOrWhiteSpace(elementName))
            {
                throw Error.ArgumentNullOrEmpty("elementName");
            }

            if (writeContext == null)
            {
                throw Error.ArgumentNull("writeContext");
            }

            List <ODataProperty> propertyCollection = null;

            if (graph != null)
            {
                propertyCollection = new List <ODataProperty>();
                foreach (IEdmProperty property in _edmComplexType.ComplexDefinition().Properties())
                {
                    IEdmTypeReference propertyType       = property.Type;
                    ODataSerializer   propertySerializer = SerializerProvider.GetEdmTypeSerializer(propertyType);
                    if (propertySerializer == null)
                    {
                        throw Error.NotSupported("Type {0} is not a serializable type", propertyType.FullName());
                    }

                    // TODO 453795: [OData]Cleanup reflection code in the ODataFormatter.
                    object propertyValue = graph.GetType().GetProperty(property.Name).GetValue(graph, index: null);

                    propertyCollection.Add(propertySerializer.CreateProperty(propertyValue, property.Name, writeContext));
                }
            }

            if (propertyCollection != null)
            {
                return(new ODataProperty()
                {
                    Name = elementName,
                    Value = new ODataComplexValue()
                    {
                        Properties = propertyCollection,
                        TypeName = _edmComplexType.FullName()
                    }
                });
            }
            else
            {
                return(null);
            }
        }
Exemple #3
0
        /// <inheritdoc/>
        public override ODataProperty CreateProperty(object graph, string elementName, ODataSerializerContext writeContext)
        {
            if (String.IsNullOrWhiteSpace(elementName))
            {
                throw Error.ArgumentNullOrEmpty("elementName");
            }

            if (writeContext == null)
            {
                throw Error.ArgumentNull("writeContext");
            }

            IEnumerable enumerable = graph as IEnumerable;

            if (enumerable == null)
            {
                return(new ODataProperty()
                {
                    Name = elementName, Value = null
                });
            }
            else
            {
                ArrayList valueCollection = new ArrayList();

                IEdmTypeReference itemType       = _edmCollectionType.ElementType();
                ODataSerializer   itemSerializer = SerializerProvider.GetEdmTypeSerializer(itemType);
                if (itemSerializer == null)
                {
                    throw Error.NotSupported(SRResources.TypeCannotBeSerialized, itemType.FullName(), typeof(ODataMediaTypeFormatter).Name);
                }

                foreach (object item in enumerable)
                {
                    valueCollection.Add(itemSerializer.CreateProperty(item, ODataFormatterConstants.Element, writeContext).Value);
                }

                // ODataCollectionValue is only a V3 property, arrays inside Complex Types or Entity types are only supported in V3
                // if a V1 or V2 Client requests a type that has a collection within it ODataLIb will throw.
                // Also, note that TypeName is an optional property for ODataCollectionValue
                return(new ODataProperty()
                {
                    Name = elementName, Value = new ODataCollectionValue {
                        Items = valueCollection
                    }
                });
            }
        }