Skip to content

Commit d469fdf

Browse files
committed
readonly members can be in fact mapped
1 parent a7bca7c commit d469fdf

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/AutoMapper/Configuration/MemberConfigurationExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public void Configure(TypeMap typeMap)
130130
var destMember = DestinationMember;
131131
if(destMember.DeclaringType.ContainsGenericParameters)
132132
{
133-
destMember = typeMap.DestinationSetters.Single(m => m.Name == destMember.Name);
133+
destMember = Array.Find(typeMap.DestinationTypeDetails.ReadAccessors, m => m.MetadataToken == destMember.MetadataToken);
134134
}
135135
var propertyMap = typeMap.FindOrCreatePropertyMapFor(destMember, typeof(TMember) == typeof(object) ? destMember.GetMemberType() : typeof(TMember));
136136
Apply(propertyMap);

src/AutoMapper/TypeMap.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ internal bool CanConstructorMap() => Profile.ConstructorMappingEnabled && !Desti
6565
public TypePair Types;
6666
public ConstructorMap ConstructorMap { get; set; }
6767
public TypeDetails SourceTypeDetails { get; private set; }
68-
private TypeDetails DestinationTypeDetails { get; set; }
68+
public TypeDetails DestinationTypeDetails { get; private set; }
6969
public Type SourceType => Types.SourceType;
7070
public Type DestinationType => Types.DestinationType;
7171
public ProfileMap Profile { get; }

src/UnitTests/OpenGenerics.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,30 @@
11
namespace AutoMapper.UnitTests;
2+
public class ReadonlyPropertiesGenerics : AutoMapperSpecBase
3+
{
4+
class Source
5+
{
6+
public InnerSource Inner;
7+
}
8+
class InnerSource
9+
{
10+
public int Value;
11+
}
12+
class Destination<T>
13+
{
14+
public readonly InnerDestination Inner = new();
15+
}
16+
class InnerDestination
17+
{
18+
public int Value;
19+
}
20+
protected override MapperConfiguration CreateConfiguration() => new(c =>
21+
{
22+
c.CreateMap(typeof(Source), typeof(Destination<>)).ForMember("Inner", o => o.MapFrom("Inner"));
23+
c.CreateMap<InnerSource, InnerDestination>();
24+
});
25+
[Fact]
26+
public void Should_work() => Map<Destination<int>>(new Source { Inner = new() { Value = 42 } }).Inner.Value.ShouldBe(42);
27+
}
228
public class ConstructorValidationGenerics : NonValidatingSpecBase
329
{
430
record Source<T>(T Value);

0 commit comments

Comments
 (0)