Skip to content

Allow mapping of generic methods in expressions irrespective of the parameter #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>1.0.3</VersionPrefix>
<VersionPrefix>1.0.4</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 @@ -69,12 +69,14 @@ TDestDelegate GetLambda(Dictionary<Type, Type> typeMappings, XpressionMapperVisi
return (TDestDelegate)Lambda
(
typeDestFunc,
ExpressionFactory.ToType(remappedBody, typeDestFunc.GetGenericArguments().Last()),
typeDestFunc.IsFuncType() ? ExpressionFactory.ToType(remappedBody, typeDestFunc.GetGenericArguments().Last()) : remappedBody,
expression.GetDestinationParameterExpressions(visitor.InfoDictionary, typeMappings)
);
}
}

private static bool IsFuncType(this Type type)
=> type.FullName.StartsWith("System.Func");

/// <summary>
/// Maps an expression given a dictionary of types where the source type is the key and the destination type is the value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,11 @@ protected override Expression VisitConstant(ConstantExpression node)
protected override Expression VisitMethodCall(MethodCallExpression node)
{
var parameterExpression = node.GetParameterExpression();
if (parameterExpression == null)
return base.VisitMethodCall(node);

InfoDictionary.Add(parameterExpression, TypeMappings);
if (parameterExpression != null)
InfoDictionary.Add(parameterExpression, TypeMappings);

var listOfArgumentsForNewMethod = node.Arguments.Aggregate(new List<Expression>(), (lst, next) =>
{
//var mappedNext = ArgumentMapper.Create(this, next).MappedArgumentExpression;
//Using VisitUnary and VisitLambda instead of ArgumentMappers
var mappedNext = this.Visit(next);
TypeMappings.AddTypeMapping(ConfigurationProvider, next.Type, mappedNext.Type);

Expand All @@ -169,11 +165,6 @@ protected override Expression VisitMethodCall(MethodCallExpression node)

ConvertTypesIfNecessary(node.Method.GetParameters(), listOfArgumentsForNewMethod, node.Method);

/*Using VisitUnary and VisitLambda instead of ArgumentMappers
* return node.Method.IsStatic
? GetStaticExpression()
: GetInstanceExpression(ArgumentMapper.Create(this, node.Object).MappedArgumentExpression);*/

return node.Method.IsStatic
? GetStaticExpression()
: GetInstanceExpression(this.Visit(node.Object));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,40 @@ public void Can_correctly_type_maps_for_child_properties()
//Assert
Assert.NotNull(dest);
}

[Fact]
public void Can_map_expressions_with_no_parameters()
{
//Arrange
Expression<Func<OptionS>> exp = () => Activator.CreateInstance<OptionS>();

//Act
Expression<Func<OptionT>> expmapped = mapper.Map<Expression<Func<OptionS>>, Expression<Func<OptionT>>>(exp);

//Assert
Assert.True(expmapped.Type == typeof(Func<OptionT>));
}


[Fact]
public void Can_map_expressions_with_action_independent_of_expression_param()
{
//Arrange
Expression<Action<OptionS>> exp = (s) => CallSomeAction<OptionS>(Activator.CreateInstance<OptionS>());

//Act
Expression<Action<OptionT>> expmapped = mapper.MapExpression<Expression<Action<OptionS>>, Expression<Action<OptionT>>>(exp);

//Assert
expmapped.Compile()(Activator.CreateInstance<OptionT>());
Assert.True(this.val.GetType() == typeof(OptionT));
}

object val;
void CallSomeAction<T>(T val)
{
this.val = val;
}
#endregion Tests

private static void SetupAutoMapper()
Expand Down Expand Up @@ -698,6 +732,16 @@ private static void SetupQueryableCollection()
private static IQueryable<User> Users { get; set; }
}

public class OptionS
{
public static OptionS GetNew() => new OptionS();
}

public class OptionT
{
public static OptionT GetNew() => new OptionT();
}

public class Account
{
public Account()
Expand Down Expand Up @@ -1066,6 +1110,8 @@ public OrganizationProfile()
.ForMember(x => x.Items, opt => opt.MapFrom(x => x.OrderData.Items));
CreateMap<ItemEntity, ItemModel>();

CreateMap<OptionT, OptionS>();

CreateMissingTypeMaps = true;
}
}
Expand Down