@@ -219,9 +219,17 @@ private static Expression SetValueByReflection(MemberMapping member, MemberExpre
219219 // Prop2 = convert(src.Prop2),
220220 //}
221221
222+ if ( arg . MapType == MapType . Projection && arg . DestinationType . IsAbstract && arg . Settings . Includes . Count > 0 )
223+ return CreateIncludeProjectionExpression ( source , arg ) ;
224+
222225 var exp = CreateInstantiationExpression ( source , arg ) ;
226+ if ( exp . NodeType == ExpressionType . Throw )
227+ return null ;
228+
223229 var memberInit = exp as MemberInitExpression ;
224- var newInstance = memberInit ? . NewExpression ?? ( NewExpression ) exp ;
230+ var newInstance = memberInit ? . NewExpression ?? exp as NewExpression ;
231+ if ( newInstance == null )
232+ return null ;
225233 var contructorMembers = newInstance . GetAllMemberExpressionsMemberInfo ( ) . ToArray ( ) ;
226234 ClassModel ? classModel ;
227235 ClassMapping ? classConverter ;
@@ -272,6 +280,36 @@ private static Expression SetValueByReflection(MemberMapping member, MemberExpre
272280 return Expression . MemberInit ( newInstance , lines ) ;
273281 }
274282
283+ static Expression CreateIncludeProjectionExpression ( Expression source , CompileArgument arg )
284+ {
285+ Expression body = Expression . Default ( arg . DestinationType ) ;
286+ foreach ( var tuple in arg . Settings . Includes )
287+ {
288+ var itemTuple = tuple ;
289+ if ( tuple . Source . IsOpenGenericType ( ) && tuple . Destination . IsOpenGenericType ( ) )
290+ {
291+ var genericArg = source . Type . GetGenericArguments ( ) ;
292+ itemTuple = new TypeTuple ( tuple . Source . MakeGenericType ( genericArg ) , tuple . Destination . MakeGenericType ( genericArg ) ) ;
293+ }
294+
295+ if ( itemTuple . Source == arg . SourceType )
296+ continue ;
297+
298+ if ( ! arg . SourceType . GetTypeInfo ( ) . IsAssignableFrom ( itemTuple . Source . GetTypeInfo ( ) ) )
299+ continue ;
300+
301+ if ( ! arg . DestinationType . GetTypeInfo ( ) . IsAssignableFrom ( itemTuple . Destination . GetTypeInfo ( ) ) )
302+ continue ;
303+
304+ var test = Expression . TypeIs ( source , itemTuple . Source ) ;
305+ var cast = Expression . TypeAs ( source , itemTuple . Source ) ;
306+ var mapped = CreateAdaptExpressionCore ( cast ! , itemTuple . Destination , arg ) ;
307+ body = Expression . Condition ( test , mapped . To ( arg . DestinationType , true ) , body ) ;
308+ }
309+
310+ return body ;
311+ }
312+
275313 protected override Expression CreateExpressionBody ( Expression source , Expression ? destination , CompileArgument arg )
276314 {
277315 TypeAdapterRule ? rule ;
0 commit comments