Esempio n. 1
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);
        }
Esempio n. 2
0
        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);
        }
        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 = SerializerHelper.RecursiveConfigureChildSerializer(childConfigurable, itemSerializer);
                        }
                    }
                    else
                    {
                        IBsonSerializer childSerializer;
                        if (PreviouslyUsedSerializerFinder.TryFindSerializer(node, childConfigurable.ChildSerializer.ValueType, out childSerializer))
                        {
                            serializer = childConfigurable.WithChildSerializer(childSerializer);
                        }
                    }
                }
            }

            return(serializer);
        }