forked from jitbit/PropMapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPropMapper.cs
193 lines (169 loc) · 7.35 KB
/
PropMapper.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
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
#nullable enable
namespace DX.Shared
{
/// <summary>
/// Static class with Extention Methods for cloning objects
/// </summary>
public static class ClassClonator
{
/// <summary>
/// Copy readable Properties to the output Instances writable properties
/// matched by equal Name
/// </summary>
/// <typeparam name="TInput"></typeparam>
/// <typeparam name="TOutput"></typeparam>
/// <param name="input">must not be null</param>
/// <param name="output">must not be null</param>
/// <returns></returns>
public static bool CopyTo<TInput, TOutput>(this TInput input, TOutput output)
{
return CopyMapper<TInput, TOutput>.CopyTo(input, output);
}
/// <summary>
/// Copy readable Properties from the input Instances to the writable properties
/// matched by equal Name
/// </summary>
/// <typeparam name="TInput"></typeparam>
/// <typeparam name="TOutput"></typeparam>
/// <param name="input"></param>
/// <param name="output"></param>
/// <returns></returns>
public static bool CopyFrom<TInput, TOutput>(this TOutput output, TInput input)
{
return CopyMapper<TInput, TOutput>.CopyTo(input, output);
}
/// <summary>
/// Craete a new Object and Copy all readable Properties to the returned Instances writable properties
/// matched by equal Name
/// </summary>
/// <typeparam name="TInput"></typeparam>
/// <typeparam name="TOutput"></typeparam>
/// <param name="input"></param>
/// <returns></returns>
public static TOutput CreateCopy<TInput, TOutput>(this TInput input) where TOutput : new()
{
if (input is null)
{
throw new ArgumentNullException(nameof(input));
}
return CloneMapper<TInput, TOutput>.From(input);
}
/// <summary>
/// returns a Copy for each Object in the input stream
/// Copy all readable Properties to the returned Instances writable properties
/// matched by equal Name
/// </summary>
/// <typeparam name="TInput"></typeparam>
/// <typeparam name="TOutput">must have a parameterless Konstruktor : new() </typeparam>
/// <param name="inputArr">must not be null</param>
/// <returns></returns>
public static IEnumerable<TOutput> CopyAll<TInput, TOutput>(this IEnumerable<TInput> inputArr) where TOutput : new()
{
foreach (TInput input in inputArr)
{
if (input is null)
{
//System.Diagnostics.Debug.Fail($"Input was null {typeof(TInput)}");
}
else
{
yield return CloneMapper<TInput, TOutput>.From(input);
}
}
}
}
internal static class PropertyCache<T>
{
static readonly IEnumerable<PropertyInfo> _WriteProps;
static readonly IEnumerable<PropertyInfo> _ReadProps;
static PropertyCache()
{
_ReadProps = typeof(T)
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(prop => prop.CanRead);
_WriteProps = typeof(T)
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(prop => prop.CanWrite);
}
public static IEnumerable<PropertyInfo> ReadProps { get { return _ReadProps; } }
public static IEnumerable<PropertyInfo> WriteProps { get { return _WriteProps; } }
}
/// <summary>
/// clones object public properties to a new object
/// uses Expressions (compiled and saved to static) - faster than Reflection
/// (compilation happens with every new generic type call cause it's a new static class each time)
/// </summary>
/// <typeparam name="TInput"></typeparam>
/// <typeparam name="TOutput"></typeparam>
internal class CloneMapper<TInput, TOutput> where TOutput : new()
{
private static readonly Func<TInput, TOutput> _cloner;
static CloneMapper()
{
_cloner = CreateCloner();
}
private static Func<TInput, TOutput> CreateCloner()
{
ParameterExpression input = Expression.Parameter(typeof(TInput), "input");
// For each property that exists in the destination object, is there a property with the same name in the source object?
IEnumerable<MemberBinding> memberBindings = PropertyCache<TInput>.ReadProps.Join(PropertyCache<TOutput>.WriteProps,
sourceProperty => sourceProperty.Name,
destinationProperty => destinationProperty.Name,
(sourceProperty, destinationProperty) =>
(MemberBinding)Expression.Bind(destinationProperty,
Expression.Property(input, sourceProperty)));
MemberInitExpression body = Expression.MemberInit(Expression.New(typeof(TOutput)), memberBindings);
Expression<Func<TInput, TOutput>> lambda = Expression.Lambda<Func<TInput, TOutput>>(body, input);
return lambda.Compile();
}
public static TOutput From(TInput input)
{
return _cloner(input);
}
}
/// <summary>
/// clones object public properties to an existing object
/// uses Expressions (compiled and saved to static) - faster than Reflection
/// (compilation happens with every new generic type call cause it's a new static class each time)
/// </summary>
internal static class CopyMapper<TInput, TOutput>
{
private static readonly Action<TInput, TOutput> _copier;
static CopyMapper()
{
_copier = CreateCopier();
}
private static Action<TInput, TOutput> CreateCopier()
{
ParameterExpression input = Expression.Parameter(typeof(TInput), "input");
ParameterExpression output = Expression.Parameter(typeof(TOutput), "output");
// For each property that exists in the destination object, is there a property with the same name in the source object?
IEnumerable<BinaryExpression> memberAssignments = PropertyCache<TInput>.ReadProps.Join(PropertyCache<TOutput>.WriteProps,
sourceProperty => sourceProperty.Name,
destinationProperty => destinationProperty.Name,
(sourceProperty, destinationProperty) =>
Expression.Assign(
Expression.Property(output, destinationProperty),
Expression.Property(input, sourceProperty)
)
);
BlockExpression body = Expression.Block(memberAssignments);
Expression<Action<TInput, TOutput>> lambda = Expression.Lambda<Action<TInput, TOutput>>(body, input, output);
return lambda.Compile();
}
public static bool CopyTo(TInput input, TOutput output)
{
if(input is null || output is null)
{
return false;
}
_copier(input, output);
return true;
}
}
}