首页 > 移动平台 > 详细

AutoMapper Console Sample

时间:2019-08-28 21:03:29      阅读:81      评论:0      收藏:0      [点我收藏+]

方法1:

#pragma warning disable 0618
            Mapper.Initialize(cfg =>
            {
                //cfg.AddProfile<MappingProfile>();
                 cfg.AddMaps(Assembly.GetExecutingAssembly(), typeof(Program).Assembly);
            });
#pragma warning restore 0618
            var source = new Source
            {
                Value1 = 5,
                Value2 = 7,
                other = "20180102",
            };
            var result = Mapper.Map<Source, Destination>(source); 
            var s = result.ToJsonString(indented: true);
public class MappingProfile: Profile
    {
        public MappingProfile()
        {
            CreateMap<AA, AD>(MemberList.Destination)
                            .ForMember(d => d.SomeValuefff, opt => opt.Ignore());
            CreateMap<Source, Destination>().ForMember(d => d.Total, opt => opt.MapFrom<CustomResolver>())
                                                .ForMember(d => d.other, opt => opt.MapFrom(new SapDateTimeResolver(), s => s.other));
        }   
    }

 方法2:

  public class Test
    {
        private readonly IMapper _mapper;
        public Test()
        {
            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<AA, AD>(MemberList.Destination) 
                            .ForMember(d => d.SomeValuefff, opt => opt.Ignore());
                cfg.CreateMap<Source, Destination>().ForMember(d => d.Total, opt => opt.MapFrom<CustomResolver>())
                                                    .ForMember(d=>d.other, opt=> opt.MapFrom(new SapDateTimeResolver(), s => s.other));
            }); 
            _mapper = config.CreateMapper(); 
            _mapper.ConfigurationProvider.AssertConfigurationIsValid();
        }
        public void Exe() 
        {
            var source = new Source
            {
                Value1 = 5,
                Value2 = 7,
                other="20180102",
            };
            var result = _mapper.Map<Source, Destination>(source);
            var s = result.ToJsonString( indented:true); 
            Console.WriteLine(s);
        }
    }

 

AutoMapper Console Sample

原文:https://www.cnblogs.com/yangzn/p/11426312.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!