Exemplo n.º 1
0
        public SerializationExpression BindProjector(ref Expression selector)
        {
            var projector = selector as SerializationExpression;

            if (selector.NodeType == ExpressionType.MemberInit || selector.NodeType == ExpressionType.New)
            {
                var serializer = SerializerBuilder.Build(selector, _serializerRegistry);

                projector = new DocumentExpression(serializer);
            }
            else if (projector == null || projector is PipelineExpression || projector is IFieldExpression || projector is ArrayIndexExpression)
            {
                var newFieldName = "__fld0";
                if (projector is IFieldExpression)
                {
                    // We don't have to do this, but it makes the output a little nicer.
                    newFieldName = ((IFieldExpression)projector).FieldName;
                }

                // the output of a $project stage must be a document, so
                // if this isn't already a serialization expression and it's not
                // a new expression or member init, then we need to create an
                // artificial field to project the computation into.
                var serializer = GetSerializer(selector.Type, selector);
                selector  = new FieldAsDocumentExpression(selector, newFieldName, serializer);
                projector = new FieldExpression(newFieldName, serializer);
            }

            return(projector);
        }
Exemplo n.º 2
0
        public IBsonSerializer GetSerializer(Type type, Expression node)
        {
            Ensure.IsNotNull(type, nameof(type));

            IBsonSerializer serializer;

            if (node != null && PreviouslyUsedSerializerFinder.TryFindSerializer(node, type, out serializer))
            {
                return(serializer);
            }
            else if (node == null || type != node.Type)
            {
                serializer = _serializerRegistry.GetSerializer(type);
                if (node != null)
                {
                    var childConfigurableSerializer = serializer as IChildSerializerConfigurable;
                    if (childConfigurableSerializer != null)
                    {
                        var nodeSerializer         = GetSerializer(node.Type, node);
                        var deepestChildSerializer = SerializerHelper.GetDeepestChildSerializer(childConfigurableSerializer);

                        if (nodeSerializer.ValueType == deepestChildSerializer.ValueType)
                        {
                            serializer = SerializerHelper.RecursiveConfigureChildSerializer(childConfigurableSerializer, nodeSerializer);
                        }
                    }
                }
            }
            else
            {
                serializer = SerializerBuilder.Build(node, _serializerRegistry);
            }

            return(serializer);
        }
        public static IBsonSerializer Build(Expression node, IBsonSerializerRegistry serializerRegistry)
        {
            var builder = new SerializerBuilder(serializerRegistry);

            return(builder.Build(node));
        }