Skip to content

Instantly share code, notes, and snippets.

@TadaoYamaoka
Last active August 7, 2019 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TadaoYamaoka/7791fcfe274b9bb2fc5a4318e2f4dd15 to your computer and use it in GitHub Desktop.
Save TadaoYamaoka/7791fcfe274b9bb2fc5a4318e2f4dd15 to your computer and use it in GitHub Desktop.
AutoMapperのサンプルプログラム
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