EmitMapper的使用
转载:http://www.cnblogs.com/aaa6818162/archive/2012/06/21/2557879.html
EmitMapper是一个开源实体映射框架,地址:http://emitmapper.codeplex.com/。
//=>一、(默认映射配置器)属性名称一样的情况;类型可以不一样,但基元素类型转换可以进行ToString。(注意是属性名称是区分大小写) //=》写法一 //ObjectsMapper<Sourse, Dest> mapper = //ObjectMapperManager.DefaultInstance.GetMapper<Sourse, Dest>(); //Dest dst = mapper.Map(src); //=》简写二 Dest dst = null; dst = ObjectMapperManager.DefaultInstance.GetMapper<Sourse, Dest>().Map(src, dst); //=》二、自定义映射配置器 ObjectsMapper<Sourse, Dest> mapper1 = new ObjectMapperManager().GetMapper<Sourse, Dest> ( new DefaultMapConfig() .IgnoreMembers<Sourse, Dest>(new string[] { "A" }) .NullSubstitution<decimal?, decimal>((value) => { return -1M; }) //如果不是语句块,则可以省略return;如果是加上return,则需要加上花括号 { return } //.NullSubstitution<decimal?, decimal>((value) => -1M) .ConvertUsing<Inner, Inner2>(value => new Inner2 { D12 = value.D1, D22 = value.D2 }) //.ConvertUsing<Inner, Inner2>(value => { return new Inner2 { D12 = value.D1, D22 = value.D2 }; }) //=>方法体的写法 //.PostProcess<Dest>((value, state) => //{ // value.F = "nothing"; // value.A = 111; // return value; //}) //); //=>return的写法 //.PostProcess<Dest>((value, state) => //{ // return new Dest // { // F = "nothing", // A = 111, // }; //}) //.PostProcess<Dest>((value, state) => // new Dest // { // F = "noting", // A = 111, // }) //); .PostProcess<Dest>((Dest value, object state) => new Dest { F = "noting", A = 111, }) ); Dest dst1 = mapper1.Map(src); //=>其他 //=>委托写法 //EmitMapper.MappingConfiguration.MappingOperations.ValuesPostProcessor<Dest> ValuesPostProcessor1 = delegate(Dest dest1, object state) { return new Dest { }; }; EmitMapper.MappingConfiguration.MappingOperations.ValuesPostProcessor<Dest> ValuesPostProcessor2 = (d, s) => { return new Dest { }; };
EmitMapper的使用小结(很不错)
http://www.cnblogs.com/ariklee/p/3833598.html
自己总结的Demo下载:
https://files.cnblogs.com/files/zfanlong1314/EmitMapperComponent.zip
Customization using default configurator
Customization overview
Custom converters
Custom converters_for_generics
Null substitution
Ignoring members
Custom constructors
Shallow and_deep_mapping
Names matching
Post processing
作者:阿笨
【官方QQ一群:跟着阿笨一起玩NET(已满)】:422315558
【官方QQ二群:跟着阿笨一起玩C#(已满)】:574187616
【官方QQ三群:跟着阿笨一起玩ASP.NET(已满)】:967920586
【官方QQ四群:Asp.Net Core跨平台技术开发(可加入)】:829227829
【官方QQ五群:.NET Core跨平台开发技术(可加入)】:647639415
【网易云课堂】:https://study.163.com/provider/2544628/index.htm?share=2&shareId=2544628
【腾讯课堂】:https://abennet.ke.qq.com
【51CTO学院】:https://edu.51cto.com/sd/66c64
【微信公众号】:微信搜索:跟着阿笨一起玩NET