Can AutoMapper implicitly flatten this mapping?

automapper

Solution

create mappings between `A` and `Destination`, and `Source` and `Destination`, and then use `AfterMap()` to use first mapping in second

Mapper.CreateMap<A, Destination>();
Mapper.CreateMap<Source, Destination>()
      .AfterMap((s, d) => Mapper.Map<A, Destination>(s.MyA, d));

then use it like this:

var res = Mapper.Map<Source, Destination>(new Source { SomeOtherValue = 7,  MyA = new A { Id = 1, Name = "SomeName" } });

Problem

I am trying to map between two lists of objects. The source type has a complex property of type `A`; the destination type is a flattened subset of type `A` plus an additional scalar property that is in the source type. ``` public class A { public int Id { get; set; } public string Name { get; set; } } public class Source { public A MyA { get; set; } public int SomeOtherValue { get; set; } } public class Destination { public string Name { get; set; } public int SomeOtherValue { get; set; } } ``` If it's not clear, I'd like `Source.MyA.Name` to map to `Destination.Name` and `Source.SomeOtherValue` to map to `Destination.SomeOtherValue`. In reality, type `A` has a dozen or so properties, about which 80% map over to properties of the same name in `Destination`. I can get things to work if I explicitly spell out the mappings in `CreateMap` like so: ``` CreateMap<Source, Destination>() .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.MyA.Name)); ``` The downside here is I want to avoid having to add a `ForMember` line for each of `A`'s properties that need to get copied over to `Destination`. I was hoping I could do something like: ``` CreateMap<Source, Destination>() .ForMember(dest => dest, opt => opt.MapFrom(src => src.MyA)); ``` But if I try the above I get a runtime error when the mapping is registered: "Custom configuration for members is only supported for top-level individual members on a type." Thanks

Original source