using AutoMapper;
using System;
using System.Collections;
using System.Collections.Generic;
namespace EC.Utils
{
///
/// ExtAutoMapper
/// 版 本:V3.0.0
/// 版 权:EasyCode
/// 作 者:LXC
///
public static class ExtAutoMapper
{
///
/// 类型映射
///
public static T MapTo(this object obj)
{
if (obj == null) return default(T);
var config = new MapperConfiguration(cfg => cfg.CreateMap(obj.GetType(), typeof(T)));
var mapper = config.CreateMapper();
return mapper.Map(obj);
}
///
/// 集合列表类型映射
///
public static List MapToList(this IEnumerable source)
{
Type sourceType = source.GetType().GetGenericArguments()[0]; //获取枚举的成员类型
var config = new MapperConfiguration(cfg => cfg.CreateMap(sourceType, typeof(TDestination)));
var mapper = config.CreateMapper();
return mapper.Map>(source);
}
///
/// 集合列表类型映射
///
public static List MapToList(this IEnumerable source)
{
var config = new MapperConfiguration(cfg => cfg.CreateMap(typeof(TSource), typeof(TDestination)));
var mapper = config.CreateMapper();
return mapper.Map>(source);
}
///
/// 类型映射
///
public static TDestination MapTo(this TSource source, TDestination destination)
where TSource : class
where TDestination : class
{
if (source == null) return destination;
var config = new MapperConfiguration(cfg => cfg.CreateMap(typeof(TSource), typeof(TDestination)));
var mapper = config.CreateMapper();
return mapper.Map(source);
}
}
}