// ***********************************************************************
// 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.Collections;
using System.Collections.Generic;
using System.Data;
namespace Infrastructure
{
public static class AutoMapperExt
{
///
/// 类型映射
///
public static T MapTo(this object obj)
{
if (obj == null) return default(T);
Mapper.CreateMap(obj.GetType(), typeof(T));
return Mapper.Map(obj);
}
///
/// 集合列表类型映射
///
public static List MapToList(this IEnumerable source)
{
foreach (var first in source)
{
var type = first.GetType();
Mapper.CreateMap(type, typeof(TDestination));
break;
}
return Mapper.Map>(source);
}
///
/// 集合列表类型映射
///
public static List MapToList(this IEnumerable source)
{
//IEnumerable 类型需要创建元素的映射
Mapper.CreateMap();
return Mapper.Map>(source);
}
///
/// 类型映射
///
public static TDestination MapTo(this TSource source, TDestination destination)
where TSource : class
where TDestination : class
{
if (source == null) return destination;
Mapper.CreateMap();
return Mapper.Map(source, destination);
}
///
/// DataReader映射
///
public static IEnumerable DataReaderMapTo(this IDataReader reader)
{
Mapper.Reset();
Mapper.CreateMap>();
return Mapper.Map>(reader);
}
}
}