示例#1
0
        public IBsonSerializer Build(Expression node)
        {
            if (node is ISerializationExpression)
            {
                return(((ISerializationExpression)node).SerializationInfo.Serializer);
            }

            IBsonSerializer serializer;

            switch (node.NodeType)
            {
            case ExpressionType.MemberInit:
                serializer = BuildMemberInit((MemberInitExpression)node);
                break;

            case ExpressionType.New:
                serializer = BuildNew((NewExpression)node);
                break;

            default:
                if (!PreviouslyUsedSerializerFinder.TryFindSerializer(node, out serializer))
                {
                    serializer = _serializerRegistry.GetSerializer(node.Type);
                }
                break;
            }

            return(serializer);
        }
示例#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 bool TryFindSerializer(Expression node, Type type, out IBsonSerializer serializer)
        {
            var finder = new PreviouslyUsedSerializerFinder(type);
            finder.Visit(node);

            serializer = finder._serializer;
            return serializer != null;
        }
示例#4
0
            public static bool TryFindSerializer(Expression node, out IBsonSerializer serializer)
            {
                var finder = new PreviouslyUsedSerializerFinder(node.Type);

                finder.Visit(node);

                serializer = finder._serializer;
                return(serializer != null);
            }
        public IBsonSerializer Build(Expression node)
        {
            if (node is ISerializationExpression)
            {
                return(((ISerializationExpression)node).Serializer);
            }

            IBsonSerializer serializer = null;

            switch (node.NodeType)
            {
            case ExpressionType.MemberInit:
                serializer = BuildMemberInit((MemberInitExpression)node);
                break;

            case ExpressionType.New:
                if (!typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(node.Type))
                {
                    serializer = BuildNew((NewExpression)node);
                }
                break;
            }

            if (serializer == null && !PreviouslyUsedSerializerFinder.TryFindSerializer(node, node.Type, out serializer))
            {
                serializer = _serializerRegistry.GetSerializer(node.Type);
                var childConfigurable = serializer as IChildSerializerConfigurable;
                if (childConfigurable != null)
                {
                    var arraySerializer = serializer as IBsonArraySerializer;
                    BsonSerializationInfo itemSerializationInfo;
                    if (arraySerializer != null && arraySerializer.TryGetItemSerializationInfo(out itemSerializationInfo))
                    {
                        IBsonSerializer itemSerializer;
                        if (PreviouslyUsedSerializerFinder.TryFindSerializer(node, itemSerializationInfo.Serializer.ValueType, out itemSerializer))
                        {
                            serializer = RecursiveConfigureChildSerializer(childConfigurable, itemSerializer);
                        }
                    }
                    else
                    {
                        IBsonSerializer childSerializer;
                        if (PreviouslyUsedSerializerFinder.TryFindSerializer(node, childConfigurable.ChildSerializer.ValueType, out childSerializer))
                        {
                            serializer = childConfigurable.WithChildSerializer(childSerializer);
                        }
                    }
                }
            }

            return(serializer);
        }
        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)
            {
                return(_serializerRegistry.GetSerializer(type));
            }

            return(SerializerBuilder.Build(node, _serializerRegistry));
        }