Last active
August 7, 2019 13:11
-
-
Save TadaoYamaoka/7791fcfe274b9bb2fc5a4318e2f4dd15 to your computer and use it in GitHub Desktop.
AutoMapperのサンプルプログラム
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using AutoMapper; | |
namespace amtest | |
{ | |
class Order { | |
public string aaa; | |
public int[] bbb; | |
public List<int> ccc; | |
public Order(string aaa, int[] bbb, List<int> ccc) { | |
this.aaa = aaa; | |
this.bbb = bbb; | |
this.ccc = ccc; | |
} | |
} | |
class OrderDto { | |
public string aaa = null; | |
public List<int> bbb = null; | |
public readonly List<float> ccc = new List<float>(); | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var config = new MapperConfiguration(cfg => { | |
cfg.CreateMap<Order, OrderDto>() | |
.AfterMap((src, dest) => { | |
foreach (var v in src.ccc) { | |
dest.ccc.Add(v); | |
} | |
}); | |
}); | |
var mapper = config.CreateMapper(); | |
var order = new Order("aaa", new int[]{1, 2, 3}, new List<int>{4, 5, 6}); | |
OrderDto dto = mapper.Map<OrderDto>(order); | |
Console.WriteLine(dto.aaa); | |
foreach (var v in dto.bbb) { | |
Console.WriteLine(v); | |
} | |
foreach (var v in dto.ccc) { | |
Console.WriteLine(v); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment