Skip to content

Commit 723d346

Browse files
authored
Fixes Issue #185. (#186)
1 parent 7fa9eb9 commit 723d346

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

src/AutoMapper.Extensions.ExpressionMapping/MapperExtensions.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,17 +260,29 @@ public static Dictionary<Type, Type> AddTypeMapping<TSource, TDest>(this Diction
260260

261261
private static bool HasUnderlyingType(this Type type)
262262
{
263-
return (type.IsGenericType() && typeof(System.Collections.IEnumerable).IsAssignableFrom(type)) || type.IsArray;
263+
return (type.IsGenericType() && type.GetGenericArguments().Length > 0) || type.IsArray;
264264
}
265265

266266
private static void AddUnderlyingTypes(this Dictionary<Type, Type> typeMappings, IConfigurationProvider configurationProvider, Type sourceType, Type destType)
267267
{
268-
typeMappings.DoAddTypeMappings
269-
(
270-
configurationProvider,
271-
!sourceType.HasUnderlyingType() ? new List<Type>() : ElementTypeHelper.GetElementTypes(sourceType).ToList(),
272-
!destType.HasUnderlyingType() ? new List<Type>() : ElementTypeHelper.GetElementTypes(destType).ToList()
273-
);
268+
if ((sourceType.IsGenericType() && typeof(System.Collections.IEnumerable).IsAssignableFrom(sourceType)) || sourceType.IsArray)
269+
{
270+
typeMappings.DoAddTypeMappings
271+
(
272+
configurationProvider,
273+
!sourceType.HasUnderlyingType() ? new List<Type>() : ElementTypeHelper.GetElementTypes(sourceType).ToList(),
274+
!destType.HasUnderlyingType() ? new List<Type>() : ElementTypeHelper.GetElementTypes(destType).ToList()
275+
);
276+
}
277+
else if (sourceType.IsGenericType() && destType.IsGenericType())
278+
{
279+
typeMappings.DoAddTypeMappings
280+
(
281+
configurationProvider,
282+
!sourceType.HasUnderlyingType() ? new List<Type>() : sourceType.GetGenericArguments().ToList(),
283+
!destType.HasUnderlyingType() ? new List<Type>() : destType.GetGenericArguments().ToList()
284+
);
285+
}
274286
}
275287

276288
/// <summary>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
3+
namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
4+
{
5+
public class SetPropertyCalls<TSource>
6+
{
7+
public SetPropertyCalls<TSource> SetProperty<TProperty>(
8+
Func<TSource, TProperty> propertyExpression,
9+
Func<TSource, TProperty> valueExpression)
10+
=> throw new InvalidOperationException();
11+
12+
13+
public SetPropertyCalls<TSource> SetProperty<TProperty>(
14+
Func<TSource, TProperty> propertyExpression,
15+
TProperty valueExpression)
16+
=> throw new InvalidOperationException();
17+
}
18+
}

tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/XpressionMapperTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,19 @@ public void Map_accountModel_to_account_with_null_checks_against_string_type()
884884
Assert.True(accounts.Count == 0);
885885
}
886886

887+
[Fact]
888+
public void Map_SetPropertyCalls()
889+
{
890+
//Arrange
891+
Expression<Func<SetPropertyCalls<AccountModel>, SetPropertyCalls<AccountModel>>> exp = s => s.SetProperty(p => p.Description, "newName");
892+
893+
//Act
894+
Expression<Func<SetPropertyCalls<Account>, SetPropertyCalls<Account>>> expMapped = mapper.MapExpression<Expression<Func<SetPropertyCalls<Account>, SetPropertyCalls<Account>>>>(exp);
895+
896+
//Assert
897+
Assert.NotNull(expMapped);
898+
}
899+
887900
[Fact]
888901
public void Map_accountModel_to_account_with_left_null_checks_against_string_type()
889902
{

0 commit comments

Comments
 (0)