@@ -168,7 +168,7 @@ internal static QualifiedMember CreateMember(
168168
169169 while ( expression . NodeType != ExpressionType . Parameter )
170170 {
171- var memberExpression = expression . GetMemberAccess ( ) ;
171+ var memberExpression = GetMemberAccess ( expression ) ;
172172 memberAccesses . Insert ( 0 , memberExpression ) ;
173173 expression = memberExpression . GetParentOrNull ( ) ;
174174 }
@@ -182,7 +182,7 @@ internal static QualifiedMember CreateMember(
182182 for ( var i = 0 ; i < memberAccesses . Count ; i ++ )
183183 {
184184 var memberAccess = memberAccesses [ i ] ;
185- var memberName = memberAccess . GetMemberName ( ) ;
185+ var memberName = GetMemberName ( memberAccess ) ;
186186 var members = membersFactory . Invoke ( parentMember . Type ) ;
187187 var member = members . FirstOrDefault ( m => m . Name == memberName ) ;
188188
@@ -197,5 +197,59 @@ internal static QualifiedMember CreateMember(
197197
198198 return QualifiedMember . From ( memberChain , mapperContext ) ;
199199 }
200+
201+ private static Expression GetMemberAccess ( Expression expression )
202+ {
203+ while ( true )
204+ {
205+ switch ( expression . NodeType )
206+ {
207+ case ExpressionType . Convert :
208+ expression = ( ( UnaryExpression ) expression ) . Operand ;
209+ continue ;
210+
211+ case ExpressionType . Call :
212+ return GetMethodCallMemberAccess ( ( MethodCallExpression ) expression ) ;
213+
214+ case ExpressionType . Lambda :
215+ expression = ( ( LambdaExpression ) expression ) . Body ;
216+ continue ;
217+
218+ case ExpressionType . MemberAccess :
219+ return expression ;
220+ }
221+
222+ throw new NotSupportedException ( "Unable to get member access from " + expression . NodeType + " Expression" ) ;
223+ }
224+ }
225+
226+ private static Expression GetMethodCallMemberAccess ( MethodCallExpression methodCall )
227+ {
228+ if ( ( methodCall . Type != typeof ( Delegate ) ) || ( methodCall . Method . Name != "CreateDelegate" ) )
229+ {
230+ return methodCall ;
231+ }
232+
233+ // ReSharper disable once PossibleNullReferenceException
234+ var methodInfo = ( MethodInfo ) ( ( ConstantExpression ) methodCall . Object ) . Value ;
235+ var instance = methodCall . Arguments . Last ( ) ;
236+ var valueParameter = Parameters . Create ( methodInfo . GetParameters ( ) . First ( ) . ParameterType , "value" ) ;
237+
238+ return Expression . Call ( instance , methodInfo , valueParameter ) ;
239+ }
240+
241+ private static string GetMemberName ( Expression expression )
242+ {
243+ switch ( expression . NodeType )
244+ {
245+ case ExpressionType . Call :
246+ return ( ( MethodCallExpression ) expression ) . Method . Name ;
247+
248+ case ExpressionType . MemberAccess :
249+ return ( ( MemberExpression ) expression ) . Member . Name ;
250+ }
251+
252+ throw new NotSupportedException ( "Unable to get member name of " + expression . NodeType + " Expression" ) ;
253+ }
200254 }
201255}
0 commit comments