Skip to content

Commit

Permalink
Merge pull request #74 from BlaiseD/master
Browse files Browse the repository at this point in the history
Improvement to map a constructor with no parameters in a selector.
  • Loading branch information
BlaiseD authored May 21, 2020
2 parents 4a69e14 + f52417d commit df59fba
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<Authors>Jimmy Bogard</Authors>
<LangVersion>latest</LangVersion>
<VersionPrefix>3.1.0</VersionPrefix>
<VersionPrefix>3.1.1-preview01</VersionPrefix>
<WarningsAsErrors>true</WarningsAsErrors>
<NoWarn>$(NoWarn);1701;1702;1591</NoWarn>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ public static ParameterExpression GetParameterExpression(this Expression express
case ExpressionType.Parameter:
return (ParameterExpression)expression;
case ExpressionType.Quote:
return GetParameterExpression(GetMemberExpression((LambdaExpression)((UnaryExpression)expression).Operand));
return GetParameterExpression(((UnaryExpression)expression).Operand);
case ExpressionType.Lambda:
return GetParameterExpression(GetMemberExpression((LambdaExpression)expression));
return GetParameterExpression(((LambdaExpression)expression).Body);
case ExpressionType.ConvertChecked:
case ExpressionType.Convert:
var ue = expression as UnaryExpression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ protected override Expression VisitLambda<T>(Expression<T> node)
return mapped;
}

protected override Expression VisitNew(NewExpression node)
{
if (this.TypeMappings.TryGetValue(node.Type, out Type newType))
return Expression.New(newType);

return base.VisitNew(node);
}

protected override Expression VisitMemberInit(MemberInitExpression node)
{
if (this.TypeMappings.TryGetValue(node.Type, out Type newType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,32 @@ public void Map__select_method_projecting_to_model_type()
Assert.True(things.Count == 2);
}

[Fact]
public void Map_MemberInit()
{
//Arrange
Expression<Func<ThingModel, ThingModel>> selection = s => new ThingModel { Car = s.Car };

//Act
Expression<Func<Thing, Thing>> selectionMapped = mapper.MapExpression<Expression<Func<Thing, Thing>>>(selection);

//Assert
Assert.NotNull(selectionMapped);
}

[Fact]
public void Map_Constructor_NoParams()
{
//Arrange
Expression<Func<ThingModel, ThingModel>> selection = s => new ThingModel();

//Act
Expression<Func<Thing, Thing>> selectionMapped = mapper.MapExpression<Expression<Func<Thing, Thing>>>(selection);

//Assert
Assert.NotNull(selectionMapped);
}

[Fact]
public void Map__select_method_where_parent_type_is_grandchild_type()
{
Expand Down

0 comments on commit df59fba

Please sign in to comment.