// ***********************************************************************
// Assembly : FairUtility
// Author : Yubao Li
// Created : 08-27-2015
//
// Last Modified By : Yubao Li
// Last Modified On : 08-27-2015
// ***********************************************************************
//
// Copyright (c) . All rights reserved.
//
//
// ***********************************************************************
using AutoMapper;
using System;
using System.Collections;
using System.Collections.Generic;
namespace djy.Service.DjyService
{
public static class AutoMapperExt
{
///
/// 类型映射
///
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);
}
}
}