Skip to content

Commit 195e81f

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

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/AutoMapper/ConstructorMap.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public class ConstructorParameterMap : MemberMap
7070
public ConstructorParameterMap(TypeMap typeMap, ParameterInfo parameter, MemberInfo[] sourceMembers) : base(typeMap)
7171
{
7272
Parameter = parameter;
73+
var parameterType = parameter.ParameterType;
74+
DestinationType = parameterType.IsByRef ? parameterType.GetElementType() : parameterType;
7375
if (sourceMembers.Length > 0)
7476
{
7577
MapByConvention(sourceMembers);
@@ -80,7 +82,7 @@ public ConstructorParameterMap(TypeMap typeMap, ParameterInfo parameter, MemberI
8082
}
8183
}
8284
public ParameterInfo Parameter { get; }
83-
public override Type DestinationType => Parameter.ParameterType;
85+
public override Type DestinationType { get; protected set; }
8486
public override IncludedMember IncludedMember { get; protected set; }
8587
public override MemberInfo[] SourceMembers { get; set; }
8688
public override string DestinationName => Parameter.Name;
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)