@@ -45,56 +45,81 @@ protected override Expression VisitMember(MemberExpression node)
4545 return base . VisitMember ( node ) ;
4646
4747 InfoDictionary . Add ( parameterExpression , TypeMappings ) ;
48+ return GetMappedMemberExpression ( node . GetBaseOfMemberExpression ( ) , new List < PropertyMapInfo > ( ) ) ;
4849
49- string sourcePath = node . GetPropertyFullName ( ) ;
50- Expression baseParentExpr = node . GetBaseOfMemberExpression ( ) ;
51- Expression visitedParentExpr = this . Visit ( baseParentExpr ) ;
52- Type sType = baseParentExpr . Type ;
53- Type dType = visitedParentExpr . Type ;
54-
55- var propertyMapInfoList = new List < PropertyMapInfo > ( ) ;
56- FindDestinationFullName ( sType , dType , sourcePath , propertyMapInfoList ) ;
57- string fullName ;
58-
59- if ( propertyMapInfoList . Any ( x => x . CustomExpression != null ) )
50+ Expression GetMappedMemberExpression ( Expression parentExpression , List < PropertyMapInfo > propertyMapInfoList )
6051 {
61- var last = propertyMapInfoList . Last ( x => x . CustomExpression != null ) ;
62- var beforeCustExpression = propertyMapInfoList . Aggregate ( new List < PropertyMapInfo > ( ) , ( list , next ) =>
63- {
64- if ( propertyMapInfoList . IndexOf ( next ) < propertyMapInfoList . IndexOf ( last ) )
65- list . Add ( next ) ;
66- return list ;
67- } ) ;
52+ Expression mappedParentExpression = this . Visit ( parentExpression ) ;
53+ FindDestinationFullName ( parentExpression . Type , mappedParentExpression . Type , node . GetPropertyFullName ( ) , propertyMapInfoList ) ;
6854
69- var afterCustExpression = propertyMapInfoList . Aggregate ( new List < PropertyMapInfo > ( ) , ( list , next ) =>
55+ if ( propertyMapInfoList . Any ( x => x . CustomExpression != null ) )
7056 {
71- if ( propertyMapInfoList . IndexOf ( next ) > propertyMapInfoList . IndexOf ( last ) )
72- list . Add ( next ) ;
73- return list ;
74- } ) ;
75-
76- fullName = BuildFullName ( beforeCustExpression ) ;
77- var visitor = new PrependParentNameVisitor
78- (
79- last . CustomExpression . Parameters [ 0 ] . Type /*Parent type of current property*/ ,
80- fullName ,
81- visitedParentExpr
82- ) ;
57+ var fromCustomExpression = GetMemberExpressionFromCustomExpression
58+ (
59+ propertyMapInfoList ,
60+ propertyMapInfoList . Last ( x => x . CustomExpression != null ) ,
61+ mappedParentExpression
62+ ) ;
63+
64+ this . TypeMappings . AddTypeMapping ( ConfigurationProvider , node . Type , fromCustomExpression . Type ) ;
65+ return fromCustomExpression ;
66+ }
8367
84- var ex = propertyMapInfoList [ propertyMapInfoList . Count - 1 ] != last
85- ? visitor . Visit ( last . CustomExpression . Body . MemberAccesses ( afterCustExpression ) )
86- : visitor . Visit ( last . CustomExpression . Body ) ;
68+ var memberExpression = GetMemberExpressionFromMemberMaps ( BuildFullName ( propertyMapInfoList ) , mappedParentExpression ) ;
69+ this . TypeMappings . AddTypeMapping ( ConfigurationProvider , node . Type , memberExpression . Type ) ;
8770
88- this . TypeMappings . AddTypeMapping ( ConfigurationProvider , node . Type , ex . Type ) ;
89- return ex ;
71+ return memberExpression ;
9072 }
91- fullName = BuildFullName ( propertyMapInfoList ) ;
92- var me = ExpressionHelpers . MemberAccesses ( fullName , visitedParentExpr ) ;
73+ }
74+
75+ protected MemberExpression GetMemberExpressionFromMemberMaps ( string fullName , Expression visitedParentExpr )
76+ => ExpressionHelpers . MemberAccesses ( fullName , visitedParentExpr ) ;
9377
94- this . TypeMappings . AddTypeMapping ( ConfigurationProvider , node . Type , me . Type ) ;
95- return me ;
78+ private Expression GetMemberExpressionFromCustomExpression ( PropertyMapInfo lastWithCustExpression ,
79+ PropertyMapInfo lastInList ,
80+ List < PropertyMapInfo > beforeCustExpression ,
81+ List < PropertyMapInfo > afterCustExpression ,
82+ Expression visitedParentExpr )
83+ {
84+ return PrependParentMemberExpression
85+ (
86+ new PrependParentNameVisitor
87+ (
88+ lastWithCustExpression . CustomExpression . Parameters [ 0 ] . Type /*Parent type of current property*/ ,
89+ BuildFullName ( beforeCustExpression ) ,
90+ visitedParentExpr
91+ )
92+ ) ;
93+
94+ Expression PrependParentMemberExpression ( PrependParentNameVisitor visitor )
95+ => visitor . Visit
96+ (
97+ lastInList != lastWithCustExpression
98+ ? lastWithCustExpression . CustomExpression . Body . MemberAccesses ( afterCustExpression )
99+ : lastWithCustExpression . CustomExpression . Body
100+ ) ;
96101 }
97102
103+ protected Expression GetMemberExpressionFromCustomExpression ( List < PropertyMapInfo > propertyMapInfoList , PropertyMapInfo lastWithCustExpression , Expression mappedParentExpr )
104+ => GetMemberExpressionFromCustomExpression
105+ (
106+ lastWithCustExpression ,
107+ propertyMapInfoList . Last ( ) ,
108+ propertyMapInfoList . Aggregate ( new List < PropertyMapInfo > ( ) , ( list , next ) =>
109+ {
110+ if ( propertyMapInfoList . IndexOf ( next ) < propertyMapInfoList . IndexOf ( lastWithCustExpression ) )
111+ list . Add ( next ) ;
112+ return list ;
113+ } ) ,
114+ propertyMapInfoList . Aggregate ( new List < PropertyMapInfo > ( ) , ( list , next ) =>
115+ {
116+ if ( propertyMapInfoList . IndexOf ( next ) > propertyMapInfoList . IndexOf ( lastWithCustExpression ) )
117+ list . Add ( next ) ;
118+ return list ;
119+ } ) ,
120+ mappedParentExpr
121+ ) ;
122+
98123 protected override Expression VisitLambda < T > ( Expression < T > node )
99124 {
100125 var ex = this . Visit ( node . Body ) ;
@@ -148,10 +173,19 @@ protected override Expression VisitUnary(UnaryExpression node)
148173
149174 Expression DoVisitUnary ( Expression updated )
150175 {
151- if ( updated != node . Operand )
152- return node . Update ( updated ) ;
153-
154- return node ;
176+ if ( this . TypeMappings . TryGetValue ( node . Type , out Type mappedType ) )
177+ return Expression . MakeUnary
178+ (
179+ node . NodeType ,
180+ updated != node . Operand
181+ ? updated
182+ : node . Operand ,
183+ mappedType
184+ ) ;
185+
186+ return updated != node . Operand
187+ ? node . Update ( updated )
188+ : base . VisitUnary ( node ) ;
155189 }
156190 }
157191
0 commit comments