/// <summary>
/// 单个对象转换
/// </summary>
/// <typeparam name="TSource">源对象泛型</typeparam>
/// <typeparam name="TDestination">目标对象泛型</typeparam>
/// <param name="source">源对象</param>
/// <returns>返回目标对象</returns>
protected TDestination ConvertObject<TSource, TDestination>(TSource source)
{
Mapper.Reset();
Mapper.Initialize(cfg => cfg.CreateMap<TSource, TDestination>());
return Mapper.Map<TDestination>(source);
}
/// <summary>
/// 对象集合转换
/// </summary>
/// <typeparam name="TSource">源对象泛型</typeparam>
/// <typeparam name="TDestination">目标对象泛型</typeparam>
/// <param name="sources">源对象集合</param>
/// <returns>返回目标对象集合</returns>
protected IList<TDestination> ConvertObjects<TSource, TDestination>(IList<TSource> sources)
{
Mapper.Reset();
Mapper.Initialize(cfg => cfg.CreateMap<TSource, TDestination>());
return Mapper.Map<IList<TDestination>>(sources);
}
原文地址:https://www.cnblogs.com/wu-peng/p/9353694.html