加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 从double映射

发布时间:2020-12-15 21:01:06 所属栏目:百科 来源:网络整理
导读:使用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 CalculationProf
使用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"));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读