Skip to content

Commit 72afbaf

Browse files
committed
Organising member extensions
1 parent ae9de5e commit 72afbaf

File tree

2 files changed

+56
-56
lines changed

2 files changed

+56
-56
lines changed

AgileMapper/Extensions/ExpressionExtensions.cs

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -30,60 +30,6 @@ public static Expression GetParentOrNull(this Expression expression)
3030
return null;
3131
}
3232

33-
public static string GetMemberName(this Expression expression)
34-
{
35-
switch (expression.NodeType)
36-
{
37-
case ExpressionType.Call:
38-
return ((MethodCallExpression)expression).Method.Name;
39-
40-
case ExpressionType.MemberAccess:
41-
return ((MemberExpression)expression).Member.Name;
42-
}
43-
44-
throw new NotSupportedException("Unable to get member name of " + expression.NodeType + " Expression");
45-
}
46-
47-
public static Expression GetMemberAccess(this Expression expression)
48-
{
49-
while (true)
50-
{
51-
switch (expression.NodeType)
52-
{
53-
case ExpressionType.Convert:
54-
expression = ((UnaryExpression)expression).Operand;
55-
continue;
56-
57-
case ExpressionType.Call:
58-
return GetMethodCallMemberAccess((MethodCallExpression)expression);
59-
60-
case ExpressionType.Lambda:
61-
expression = ((LambdaExpression)expression).Body;
62-
continue;
63-
64-
case ExpressionType.MemberAccess:
65-
return expression;
66-
}
67-
68-
throw new NotSupportedException("Unable to get member access from " + expression.NodeType + " Expression");
69-
}
70-
}
71-
72-
private static Expression GetMethodCallMemberAccess(MethodCallExpression methodCall)
73-
{
74-
if ((methodCall.Type != typeof(Delegate)) || (methodCall.Method.Name != "CreateDelegate"))
75-
{
76-
return methodCall;
77-
}
78-
79-
// ReSharper disable once PossibleNullReferenceException
80-
var methodInfo = (MethodInfo)((ConstantExpression)methodCall.Object).Value;
81-
var instance = methodCall.Arguments.Last();
82-
var valueParameter = Parameters.Create(methodInfo.GetParameters().First().ParameterType, "value");
83-
84-
return Expression.Call(instance, methodInfo, valueParameter);
85-
}
86-
8733
public static Expression AndTogether(this ICollection<Expression> expressions)
8834
{
8935
if (expressions.Count == 0)

AgileMapper/Members/MemberExtensions.cs

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)