Skip to content

Instantly share code, notes, and snippets.

@TadaoYamaoka
Last active Aug 7, 2019
Embed
What would you like to do?
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