4

Automapper 5.0.2.0 を使用して、TypeA から TypeB にマップしようとしています。

public class TypeA
{
    public double Length { get; set; }
}


public class TypeB
{
    public Distance Length { get; set; }
}

長さはインチ単位で格納されていると仮定し、次のマッピング プロファイルを作成しました。

public class CalculationProfile : Profile
{
    public CalculationProfile()
    {
       CreateMap<TypeA, TypeB>()
            .ForMember(dest => dest.Length,
                       opt => opt.MapFrom(src => new Distance(src.Length, "Inch")))
    }
}

そして私はそれを次のように使用します:

Mapper.Initialize(configuration =>
{
    configuration.AddProfile(new CalculationProfile());
});

var typeA = new TypeA(){Length = 1.0};

var typeB = Mapper.Map<TypeB>(typeA);

ただし、最後の行で次のエラーがスローされます。

AutoMapper.AutoMapperMappingException was unhandled by user code
  HResult=-2146233088
  Message=Missing type map configuration or unsupported mapping.

Mapping types:
Double -> Distance
System.Double -> UnitClassLibrary.DistanceUnit.Distance
  Source=Anonymously Hosted DynamicMethods Assembly
  StackTrace:
       at lambda_method(Closure , Double , Distance , ResolutionContext )
       at lambda_method(Closure , Object , Object , ResolutionContext )
       at TestProject.AutoMapperTests.FromPersistenceObjectToCalculationModel_Test() in C:\...\AutoMapperTests.cs:line 69
  InnerException: 

これは、Automapper が処理する非常に単純なケースのように思えますが、エラーを修正できないようです。任意の提案をいただければ幸いです。

4

1 に答える 1