-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathMapper.cs
26 lines (21 loc) · 864 Bytes
/
Mapper.cs
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
namespace AutoMapper;
public static class Mapper
{
public static TResult Map<TIn, TResult>(TIn obj) where TResult : new()
{
var result = new TResult();
var inputProperties = typeof(TIn).GetProperties();
var resultProperties = typeof(TResult).GetProperties();
foreach (var inputProperty in inputProperties)
{
// Find the property that has the same name and type
var resultProperty = resultProperties.FirstOrDefault(prop => prop.Name == inputProperty.Name && prop.PropertyType == inputProperty.PropertyType);
// If it isn't writeable, don't try to write the value
if (resultProperty != null && resultProperty.CanWrite)
{
resultProperty.SetValue(result, inputProperty.GetValue(obj));
}
}
return result;
}
}