AutoMapper Console Sample

方法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

上一篇:hadoop MapReduce自定义分区Partition输出各运营商的手机号码


下一篇:js throttle 节流