首先引入 automapper.dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AutoMapper
{
class Program
{
static void Main(string[] args)
{
var order = new Order()
{
Id=1,
OrderName="F",
ClientName ="Aven",
ClientId =2
};
var orderBrief = OrderBrief.Create(order);
}
}
class Order
{
public Int32 Id { get; set; }
public string OrderName { get; set; }
public string ClientName { get; set; }
public Int32 ClientId { get; set; }
}
class OrderBrief
{
#region Mapper实现对应
static OrderBrief()
{
var map = Mapper
.CreateMap() // 默认自动映射所有命名一致的属性。
.ForMember(x => x.OrderId , y => y.MapFrom(z => z.Id )); // 显式映射不同名字。
map.BeforeMap((user, UserModel) =>
{
Console.WriteLine("做一些预处理");
});
map.AfterMap((user, UserModel) =>
{
Console.WriteLine("做一些后续处理");
});
}
///
///
///
///
public static OrderBrief Create(Order Order)
{
return Mapper.Map(Order);
}
#endregion
public Int32 OrderId { get; set; }
public string OrderName { get; set; }
public string ClientName { get; set; }
}
}