@@ -67,11 +67,15 @@ public static TDestDelegate MapExpression<TDestDelegate>(this IMapper mapper, La
6767 return mapper . MapExpression < TDestDelegate >
6868 (
6969 expression ,
70- ( config , mappings ) => new XpressionMapperVisitor ( mapper , config , mappings )
70+ ( config , mappings ) => new XpressionMapperVisitor ( mapper , config , mappings ) ,
71+ ( desType ) => IsFuncType ( desType )
7172 ) ;
7273 }
7374
74- private static TDestDelegate MapExpression < TDestDelegate > ( this IMapper mapper , LambdaExpression expression , Func < IConfigurationProvider , Dictionary < Type , Type > , XpressionMapperVisitor > getVisitor )
75+ private static TDestDelegate MapExpression < TDestDelegate > ( this IMapper mapper ,
76+ LambdaExpression expression ,
77+ Func < IConfigurationProvider , Dictionary < Type , Type > , XpressionMapperVisitor > getVisitor ,
78+ Func < Type , bool > shouldConvertMappedBodyToDestType )
7579 where TDestDelegate : LambdaExpression
7680 {
7781 return MapExpression < TDestDelegate >
@@ -80,15 +84,17 @@ private static TDestDelegate MapExpression<TDestDelegate>(this IMapper mapper, L
8084 expression ,
8185 expression . GetType ( ) . GetGenericArguments ( ) [ 0 ] ,
8286 typeof ( TDestDelegate ) . GetGenericArguments ( ) [ 0 ] ,
83- getVisitor
87+ getVisitor ,
88+ shouldConvertMappedBodyToDestType
8489 ) ;
8590 }
8691
8792 private static TDestDelegate MapExpression < TDestDelegate > ( IConfigurationProvider configurationProvider ,
8893 LambdaExpression expression ,
8994 Type typeSourceFunc ,
9095 Type typeDestFunc ,
91- Func < IConfigurationProvider , Dictionary < Type , Type > , XpressionMapperVisitor > getVisitor )
96+ Func < IConfigurationProvider , Dictionary < Type , Type > , XpressionMapperVisitor > getVisitor ,
97+ Func < Type , bool > shouldConvertMappedBodyToDestType )
9298 where TDestDelegate : LambdaExpression
9399 {
94100 return CreateVisitor ( new Dictionary < Type , Type > ( ) . AddTypeMappingsFromDelegates ( configurationProvider , typeSourceFunc , typeDestFunc ) ) ;
@@ -99,15 +105,15 @@ TDestDelegate CreateVisitor(Dictionary<Type, Type> typeMappings)
99105 TDestDelegate MapBody ( Dictionary < Type , Type > typeMappings , XpressionMapperVisitor visitor )
100106 => GetLambda ( typeMappings , visitor , visitor . Visit ( expression . Body ) ) ;
101107
102- TDestDelegate GetLambda ( Dictionary < Type , Type > typeMappings , XpressionMapperVisitor visitor , Expression remappedBody )
108+ TDestDelegate GetLambda ( Dictionary < Type , Type > typeMappings , XpressionMapperVisitor visitor , Expression mappedBody )
103109 {
104- if ( remappedBody == null )
110+ if ( mappedBody == null )
105111 throw new InvalidOperationException ( Resource . cantRemapExpression ) ;
106112
107113 return ( TDestDelegate ) Lambda
108114 (
109115 typeDestFunc ,
110- typeDestFunc . IsFuncType ( ) ? ExpressionFactory . ToType ( remappedBody , typeDestFunc . GetGenericArguments ( ) . Last ( ) ) : remappedBody ,
116+ shouldConvertMappedBodyToDestType ( typeDestFunc ) ? ExpressionFactory . ToType ( mappedBody , typeDestFunc . GetGenericArguments ( ) . Last ( ) ) : mappedBody ,
111117 expression . GetDestinationParameterExpressions ( visitor . InfoDictionary , typeMappings )
112118 ) ;
113119 }
@@ -145,7 +151,8 @@ public static TDestDelegate MapExpressionAsInclude<TDestDelegate>(this IMapper m
145151 return mapper . MapExpression < TDestDelegate >
146152 (
147153 expression ,
148- ( config , mappings ) => new MapIncludesVisitor ( mapper , config , mappings )
154+ ( config , mappings ) => new MapIncludesVisitor ( mapper , config , mappings ) ,
155+ desType => false
149156 ) ;
150157 }
151158
0 commit comments