• AutoMapper,对象映射的简单使用


    using AutoMapper;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace MyMapperTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                Action<IMapperConfigurationExpression> configurationExpress = createMapperConfigurationExpress();
                //Mapper.Initialize(c => configurationExpress(c));
                //以下代码和第二行的Mapper.Initialize(c => configurationExpress(c));作用相同
                Mapper.Initialize(cfg => {
                    cfg.CreateMap<AddressDto, Address>();
                    cfg.CreateMap<Address, AddressDto>();
                });
    
                //单个映射实例
                //Mapper.Initialize(p => p.CreateMap<AddressDto, Address>());
                AddressDto dto = new AddressDto
                {
                    Country = "China",
                    City = "ShangHai",
                    Street = "JinZhong Street"
                };
                Address address = Mapper.Map<AddressDto, Address>(dto);
    
                AddressDto dto2 = Mapper.Map<Address, AddressDto>(address);
            }
    
            public static Action<IMapperConfigurationExpression> createMapperConfigurationExpress()
            {
                return new Action<IMapperConfigurationExpression>(cfg =>
                    {
                        cfg.CreateMap<AddressDto, Address>();
                        cfg.CreateMap<Address, AddressDto>();
                    }
                );
            }
        }
    
        public class AddressDto
        {
            public string Country { get; set; }
            public string City { get; set; }
            public string Street { get; set; }
        }
    
        public class Address
        {
            public string Country { get; set; }
            public string City { get; set; }
            public string Street { get; set; }
        }
    }
  • 相关阅读:
    埃及分数问题(JLNUOJ 2372)
    哈夫曼树的简单实现(JLOJ2370)
    4 Values whose Sum is 0 UVA 1152
    UVA 1600 Patrol Robot
    UVA122 二叉树的层次遍历
    51nod(1174 区间中最大的数)(ST表模板题)
    51nod(1089 最长回文子串 V2)(hash 加二分)
    中国剩余定理(费马小定理求逆元)
    hdu1005 矩阵快速幂
    八连通(vector动态数组法)
  • 原文地址:https://www.cnblogs.com/itjeff/p/9869364.html
Copyright © 2020-2023  润新知