Skip to content

Commit

Permalink
Merge pull request #101 from AutoMapper/SupportIncludeMembers
Browse files Browse the repository at this point in the history
Support include members
  • Loading branch information
BlaiseD authored Nov 20, 2020
2 parents d49d54f + 66d4310 commit 9f1dc09
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ public static string GetPropertyFullName(this Expression expression)
}
}

private static Expression GetUnconvertedMemberExpression(this Expression expression)
public static Expression GetUnconvertedExpression(this Expression expression)
{
switch (expression.NodeType)
{
case ExpressionType.Convert:
case ExpressionType.ConvertChecked:
case ExpressionType.TypeAs:
return ((UnaryExpression)expression).Operand.GetUnconvertedMemberExpression();
return ((UnaryExpression)expression).Operand.GetUnconvertedExpression();
default:
return expression;
}
Expand All @@ -85,18 +85,18 @@ public static Expression ConvertTypeIfNecessary(this Expression expression, Type
if (memberType == expression.Type)
return expression;

expression = expression.GetUnconvertedMemberExpression();
expression = expression.GetUnconvertedExpression();
if (memberType != expression.Type)
return Expression.Convert(expression, memberType);

return expression;
}

public static MemberExpression GetMemberExpression(this LambdaExpression expr)
=> expr.Body.GetUnconvertedMemberExpression() as MemberExpression;
=> expr.Body.GetUnconvertedExpression() as MemberExpression;

public static MemberExpression GetMemberExpression(this Expression expr)
=> expr.GetUnconvertedMemberExpression() as MemberExpression;
=> expr.GetUnconvertedExpression() as MemberExpression;

/// <summary>
/// Returns the ParameterExpression for the LINQ parameter.
Expand Down Expand Up @@ -181,7 +181,7 @@ public static string GetMemberFullName(this LambdaExpression expr)
case ExpressionType.Convert:
case ExpressionType.ConvertChecked:
case ExpressionType.TypeAs:
me = expr.Body.GetUnconvertedMemberExpression() as MemberExpression;
me = expr.Body.GetUnconvertedExpression() as MemberExpression;
break;
default:
me = expr.Body as MemberExpression;
Expand Down
15 changes: 13 additions & 2 deletions src/AutoMapper.Extensions.ExpressionMapping/MapperExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AutoMapper.Internal;
using AutoMapper.Extensions.ExpressionMapping.Extensions;
using AutoMapper.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -113,9 +114,19 @@ TDestDelegate GetLambda(Dictionary<Type, Type> typeMappings, XpressionMapperVisi
return (TDestDelegate)Lambda
(
typeDestFunc,
shouldConvertMappedBodyToDestType(typeDestFunc) ? ExpressionFactory.ToType(mappedBody, typeDestFunc.GetGenericArguments().Last()) : mappedBody,
ConvertBody(),
expression.GetDestinationParameterExpressions(visitor.InfoDictionary, typeMappings)
);

Expression ConvertBody()
{
if (!shouldConvertMappedBodyToDestType(typeDestFunc))
return mappedBody;

mappedBody = mappedBody.GetUnconvertedExpression();

return ExpressionFactory.ToType(mappedBody, typeDestFunc.GetGenericArguments().Last());
}
}
}

Expand Down

0 comments on commit 9f1dc09

Please sign in to comment.