Skip to content

Commit 616f4bd

Browse files
committed
Fix Issue #4502 - Confusing exception when trying top map types with in parameters in ctor
1 parent 2314ed6 commit 616f4bd

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

src/AutoMapper/ConstructorMap.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public class ConstructorParameterMap : MemberMap
7070
public ConstructorParameterMap(TypeMap typeMap, ParameterInfo parameter, MemberInfo[] sourceMembers) : base(typeMap)
7171
{
7272
Parameter = parameter;
73+
DestinationType = parameter.ParameterType.IsByRef ? parameter.ParameterType.GetElementType() : parameter.ParameterType;
7374
if (sourceMembers.Length > 0)
7475
{
7576
MapByConvention(sourceMembers);
@@ -80,7 +81,7 @@ public ConstructorParameterMap(TypeMap typeMap, ParameterInfo parameter, MemberI
8081
}
8182
}
8283
public ParameterInfo Parameter { get; }
83-
public override Type DestinationType => Parameter.ParameterType;
84+
public override Type DestinationType { get; protected set; }
8485
public override IncludedMember IncludedMember { get; protected set; }
8586
public override MemberInfo[] SourceMembers { get; set; }
8687
public override string DestinationName => Parameter.Name;

src/AutoMapper/Internal/TypePair.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,19 @@ public MapRequest(TypePair types) : this(types, types, MemberMap.Instance) { }
77
public override int GetHashCode() => HashCode.Combine(RequestedTypes, RuntimeTypes);
88
}
99
[DebuggerDisplay("{SourceType.Name}, {DestinationType.Name}")]
10-
public readonly record struct TypePair(Type SourceType, Type DestinationType)
10+
public readonly record struct TypePair
1111
{
12+
public TypePair(Type sourceType, Type destinationType)
13+
{
14+
SourceType = sourceType;
15+
DestinationType = destinationType.IsByRef ? destinationType.GetElementType() : destinationType;
16+
}
17+
1218
public bool IsConstructedGenericType => SourceType.IsConstructedGenericType || DestinationType.IsConstructedGenericType;
1319
public bool ContainsGenericParameters => SourceType.ContainsGenericParameters || DestinationType.ContainsGenericParameters;
20+
public Type DestinationType { get; init; }
21+
public Type SourceType { get; init; }
22+
1423
public TypePair CloseGenericTypes(TypePair closedTypes)
1524
{
1625
var sourceArguments = closedTypes.SourceType.GenericTypeArguments;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace AutoMapper.UnitTests.Bug;
2+
3+
public class ByrefConstructorParameter : AutoMapperSpecBase
4+
{
5+
private Destination _destination;
6+
7+
class Source
8+
{
9+
public TimeSpan X { get; set; }
10+
}
11+
12+
class Destination
13+
{
14+
public Destination(in TimeSpan x)
15+
{
16+
Y = x;
17+
}
18+
19+
public TimeSpan Y { get; }
20+
}
21+
22+
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
23+
{
24+
cfg.CreateMap<Source, Destination>();
25+
});
26+
27+
protected override void Because_of()
28+
{
29+
var source = new Source
30+
{
31+
X = TimeSpan.FromSeconds(17)
32+
};
33+
_destination = Mapper.Map<Destination>(source);
34+
}
35+
36+
[Fact]
37+
public void should_just_work()
38+
{
39+
_destination.Y.ShouldBe(TimeSpan.FromSeconds(17));
40+
}
41+
}

0 commit comments

Comments
 (0)