微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 从double映射

使用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,ResolutionContext )
       at TestProject.AutoMapperTests.FromPersistenceObjectToCalculationModel_test() in C:\...\AutoMapperTests.cs:line 69
  InnerException:

对于Automapper来说,这似乎是一个非常简单的案例,但是我似乎无法修复错误.任何建议表示赞赏.

解决方法

你真的想要在double和distance之间进行新的类型转换:

CreateMap<double,distance>().ConvertUsing(src => new distance(src,"Inch"));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐