このコードは、いくつかの詳細が曖昧であり、ここにコンパイラーがありませんが、開始する必要があります。これは、合理的な仮定を行い、カスタム型コンバーターを使用します。これが登録されると、入力オブジェクトからマップされたオブジェクトにマップするたびに実際の変換を行うために使用されます。
public class CarTypeConverter : ITypeConverter<input, mapped>
{
public mapped Convert(ResolutionContext context)
{
// get the input object from the context
input inputCar = (input)context.SourceValue;
// get the main car
CarDTO mappedMainCar = Mapper.Map<Car, CarDTO>(input.mainCar);
mappedMainCar.carType = EnumCarType.Main;
// create a list with the main car, then add the rest
var mappedCars = new List<CarDTO> { mappedMainCar };
mappedCars.AddRange(Mapper.Map<Car, CarDTO>(inputCar.otherCars));
return new mapped { cars = mappedCars };
}
}
// In Automapper initialization
mapperCfg.CreateMap<input, mapped>().ConvertUsing<CarTypeConverter>();
mapperCfg.CreateMap<Car, CarDTO>()
.ForMember(dest => dest.carType, opt => EnumCarType.Other);