forked from itdos/Dos.Common
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapperHelper.cs
More file actions
63 lines (60 loc) · 2.01 KB
/
Copy pathMapperHelper.cs
File metadata and controls
63 lines (60 loc) · 2.01 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Dos.Common
{
public class MapperHelper
{
public static TTo Map<TFrom, TTo>(TFrom from)
{
if (typeof(TFrom) == Types.Object)
{
return EntityCopy<TTo>(from);
}
var mapper = EmitMapper.ObjectMapperManager.DefaultInstance.GetMapper<TFrom, TTo>();
return mapper.Map(from);
}
public static List<TTo> Map<TFrom, TTo>(List<TFrom> from)
{
if (typeof(TFrom) == Types.Object)
{
return EntityCopy<TFrom,TTo>(from);
}
var mapper = EmitMapper.ObjectMapperManager.DefaultInstance.GetMapper<List<TFrom>, List<TTo>>();
return mapper.Map(from);
}
private static TResult EntityCopy<TResult>(object input)
{
if (input == null)
{
return default(TResult);
}
if (input.GetType() == typeof(TResult))
{
return (TResult)input;
}
return (TResult)EntityCopy(input, typeof(TResult));
}
private static List<TResult> EntityCopy<TEntity, TResult>(IList<TEntity> input)
{
return input.Select(entity => EntityCopy<TResult>(entity)).ToList();
}
private static object EntityCopy(object input, Type targetType)
{
//emitmapper
var objResult = Activator.CreateInstance(targetType);
var properties = targetType.GetProperties();
var type = input.GetType();
foreach (var info in properties)
{
if (!info.CanWrite) continue;
var property = type.GetProperty(info.Name);
if (property == null) continue;
var objTemp = property.GetValue(input, null);
info.SetValue(objResult, objTemp, null);
}
return objResult;
}
}
}