lixiaojun
2023-04-12 2be5d90e96f163c67101571f6865b17effcb0f3f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace IStation
{
    /// <summary>
    /// AutoMapper帮助类
    /// </summary>
    public static class AutoMapperHelper
    {
        /// <summary>
        /// 映射单项
        /// </summary>
        public static Out MapTo<In, Out>(this In obj)
        {
            var mapper = new MapperConfiguration(cfg => cfg.CreateMap<In, Out>()).CreateMapper();
            return mapper.Map<In, Out>(obj);
        }
 
        /// <summary>
        /// 映射单项
        /// </summary>
        public static void Map<In, Out>(this In inner, Out outer)
        {
            var mapper = new MapperConfiguration(cfg => cfg.CreateMap<In, Out>()).CreateMapper();
            mapper.Map(inner, outer);
        }
 
        /// <summary>
        /// 映射集合
        /// </summary>
        public static List<Out> MapTo<In, Out>(this IEnumerable<In> objs)
        {
            var mapper = new MapperConfiguration(cfg => cfg.CreateMap<In, Out>()).CreateMapper();
            return mapper.Map<IEnumerable<In>, List<Out>>(objs);
        }
 
        /// <summary>
        /// 映射集合
        /// </summary>
        public static void Map<In, Out>(this IEnumerable<In> inners, IEnumerable<Out> outers)
        {
            var mapper = new MapperConfiguration(cfg => cfg.CreateMap<In, Out>()).CreateMapper();
            mapper.Map(inners, outers);
        }
 
    }
}