2016-01-05 17:14:10 +08:00
|
|
|
|
// ***********************************************************************
|
2015-10-26 21:58:12 +08:00
|
|
|
|
// Assembly : FairUtility
|
|
|
|
|
// Author : Yubao Li
|
|
|
|
|
// Created : 08-27-2015
|
|
|
|
|
//
|
|
|
|
|
// Last Modified By : Yubao Li
|
|
|
|
|
// Last Modified On : 08-27-2015
|
|
|
|
|
// ***********************************************************************
|
|
|
|
|
// <copyright file="AutoMapperExt.cs" company="">
|
|
|
|
|
// Copyright (c) . All rights reserved.
|
|
|
|
|
// </copyright>
|
|
|
|
|
// <summary></summary>
|
|
|
|
|
// ***********************************************************************
|
|
|
|
|
|
2020-10-22 14:59:36 +08:00
|
|
|
|
using System;
|
2015-10-26 21:58:12 +08:00
|
|
|
|
using AutoMapper;
|
2015-12-03 23:39:27 +08:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
2015-10-26 21:58:12 +08:00
|
|
|
|
|
|
|
|
|
namespace Infrastructure
|
|
|
|
|
{
|
2015-12-03 23:39:27 +08:00
|
|
|
|
public static class AutoMapperExt
|
2015-10-26 21:58:12 +08:00
|
|
|
|
{
|
2015-12-03 23:39:27 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 类型映射
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static T MapTo<T>(this object obj)
|
2015-10-26 21:58:12 +08:00
|
|
|
|
{
|
2015-12-03 23:39:27 +08:00
|
|
|
|
if (obj == null) return default(T);
|
2020-10-22 14:59:36 +08:00
|
|
|
|
|
2025-07-16 21:18:29 +08:00
|
|
|
|
// 当Id为空时,不进行映射
|
|
|
|
|
var config = new MapperConfiguration(cfg => {
|
|
|
|
|
cfg.CreateMap(obj.GetType(), typeof(T))
|
|
|
|
|
.ForMember("Id", opt => opt.Condition((src, dest, srcValue) =>
|
|
|
|
|
srcValue != null && !string.IsNullOrEmpty(srcValue.ToString())
|
|
|
|
|
));
|
|
|
|
|
});
|
2020-10-22 14:59:36 +08:00
|
|
|
|
var mapper = config.CreateMapper();
|
|
|
|
|
return mapper.Map<T>(obj);
|
2015-12-03 23:39:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 集合列表类型映射
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static List<TDestination> MapToList<TDestination>(this IEnumerable source)
|
|
|
|
|
{
|
2020-10-22 14:59:36 +08:00
|
|
|
|
Type sourceType = source.GetType().GetGenericArguments()[0]; //获取枚举的成员类型
|
|
|
|
|
var config = new MapperConfiguration(cfg => cfg.CreateMap(sourceType, typeof(TDestination)));
|
|
|
|
|
var mapper = config.CreateMapper();
|
|
|
|
|
|
|
|
|
|
return mapper.Map<List<TDestination>>(source);
|
2015-12-03 23:39:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 集合列表类型映射
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static List<TDestination> MapToList<TSource, TDestination>(this IEnumerable<TSource> source)
|
|
|
|
|
{
|
2020-10-22 14:59:36 +08:00
|
|
|
|
var config = new MapperConfiguration(cfg => cfg.CreateMap(typeof(TSource), typeof(TDestination)));
|
|
|
|
|
var mapper = config.CreateMapper();
|
|
|
|
|
|
|
|
|
|
return mapper.Map<List<TDestination>>(source);
|
2015-12-03 23:39:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 类型映射
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static TDestination MapTo<TSource, TDestination>(this TSource source, TDestination destination)
|
|
|
|
|
where TSource : class
|
|
|
|
|
where TDestination : class
|
|
|
|
|
{
|
|
|
|
|
if (source == null) return destination;
|
|
|
|
|
|
2020-10-22 14:59:36 +08:00
|
|
|
|
var config = new MapperConfiguration(cfg => cfg.CreateMap(typeof(TSource), typeof(TDestination)));
|
|
|
|
|
var mapper = config.CreateMapper();
|
|
|
|
|
return mapper.Map<TDestination>(source);
|
2015-10-26 21:58:12 +08:00
|
|
|
|
}
|
2020-10-22 14:59:36 +08:00
|
|
|
|
|
2015-10-26 21:58:12 +08:00
|
|
|
|
}
|
2015-12-03 23:39:27 +08:00
|
|
|
|
}
|