using AutoMapper;
namespace WindowsFormsApp1
{
///
/// 模仿需要转换的数据实体类
///
public class Source
{
public int SomeValue { get; set; }
public string AnotherValue { get; set; }
}
///
/// 模仿需要转换的数据实体类
///
public class Destination
{
public int SomeValue { get; set; }
}
///
/// 通过Profile注册
/// 告诉AutoMapper需要进行哪些类的转换,如果A转换成B,B也要转换成A,那就需要写两条
///
public class SourceProfile : Profile
{
public SourceProfile()
{
CreateMap();
CreateMap();
}
}
}
using AutoMapper;
namespace WindowsFormsApp1
{
public class AutoMapperConfigure
{
public static IMapper GetMapper()
{
var config = new MapperConfiguration(cfg =>
{
// 扫描当前程序集
cfg.AddMaps(System.AppDomain.CurrentDomain.GetAssemblies());
});
var mapper = config.CreateMapper();
return mapper;
}
}
}