zoukankan      html  css  js  c++  java
  • 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; }
        }
    }
  • 相关阅读:
    损失函数相关
    半监督学习
    自监督学习
    leetcode相关
    深度学习中的Normalization
    TCN
    用户行为序列相关
    损失函数loss相关
    MapReduce编程之实例分析:wordCount
    MapReduce编程之初学概念篇
  • 原文地址:https://www.cnblogs.com/itjeff/p/9869364.html
Copyright © 2011-2022 走看看