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")); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ruby-on-rails – 在Rails中上传文件提供String filename而
- JsonCPP在linux下的一个奇怪的问题
- ruby-on-rails – Rails 3验证:presence => false
- 两种高性能I/O模式 reactor & proactor
- 是否可以在cmake中包含生成的make-style(不是cmake-style)依
- 我的一个朋友过来面试引发我要说的一个小话题
- 使用Xcode 4.3.2在iOS 3.1.3设备上进行调试时应用程序挂起
- S5PV210(TQ210)学习笔记——Nand flash驱动编写
- Oracle的游标使用方法 三种循环
- 对数组进行各种操作