抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

主要思想:

  • 两个不同的类进行映射
  • 利用for循环找到相同的属性
  • 利用表达式把属性的赋值过程给存下来生成 表达式list
  • 真正使用的时候, 只需要读取表达式list进行赋值就好
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
namespace Common
{

using NLog;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

public static class Mapper<TSource, TDestination>
where TSource : class
where TDestination : class, new()
{

private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();

private static readonly Type[] NotReferenceType = new Type[] { typeof(Google.Protobuf.WellKnownTypes.Timestamp) };

public readonly static Func<TSource, TDestination> MapperFunc = GetMapperFunc();

public readonly static Action<TSource, TDestination> MapperAction = GetMapperAction();

public static TDestination MapTo(TSource source, Action<TSource, TDestination> action = null)
{
if (source == null)
{
Logger.Warn("The parameter ('source') cannot be null.");
return null;
}

var target = MapperFunc(source);
action?.Invoke(source, target);
return target;
}

public static List<TDestination> MapToList(IEnumerable<TSource> sources, Action<TSource, TDestination> action = null)
{
if (sources == null)
{
Logger.Error($"The parameter ('sources') cannot be null.");
throw new ArgumentNullException($"The parameter('sources') cannot be null.");
}

var list = new List<TDestination>();
foreach (var item in sources)
{
list.Add(MapTo(item, action));
}
return list;
}

#region Private Method
private static Func<TSource, TDestination> GetMapperFunc()
{
return source =>
{
var target = new TDestination();
MapperAction(source, target);
return target;
};
}

private static Action<TSource, TDestination> GetMapperAction()
{
//获取源类型和指向类型
var sourceType = typeof(TSource);
var destinationType = typeof(TDestination);

//创建Expression参数
var sourceParameter = Expression.Parameter(sourceType, "x");
var destinationParameter = Expression.Parameter(destinationType, "p");

//创建Expression集合
var expressionList = new List<Expression>();

//获取TDestination中的所有允许写入权限的Public属性 或 RepeatedField 属性
var destinationProperties = destinationType.GetProperties()
.Where(x => (x.PropertyType.IsPublic || x.PropertyType.IsNestedPublic)
&& (x.CanWrite || x.PropertyType.FullName.Contains(typeof(Google.Protobuf.Collections.RepeatedField<>).FullName)));

var sourceProperties = sourceType.GetProperties()
.Where(x => x.PropertyType.IsPublic || x.PropertyType.IsNestedPublic);
foreach (var destinationPropertyInfo in destinationProperties)
{
try
{
//获取匹配的TSource Property
var sourcePropertyInfo = sourceProperties.FirstOrDefault(x => string.Compare(x.Name, destinationPropertyInfo.Name) == 0);
if (sourcePropertyInfo == null || !sourcePropertyInfo.CanRead || sourcePropertyInfo.PropertyType.IsNotPublic)
{
continue;
}
//创建访问属性的表达式
var sourceProperty = Expression.Property(sourceParameter, sourcePropertyInfo);
var destinationProperty = Expression.Property(destinationParameter, destinationPropertyInfo);

//TSource Property不是值类型,并且和TDestination Property不相同
if (!sourcePropertyInfo.PropertyType.IsValueType
&& !NotReferenceType.Contains(sourcePropertyInfo.PropertyType)
&& sourcePropertyInfo.PropertyType != destinationPropertyInfo.PropertyType
&& sourcePropertyInfo.PropertyType != typeof(string)
&& destinationPropertyInfo.PropertyType != typeof(string))
{
//TSource Property和TDestination Property都是Class
if (sourcePropertyInfo.PropertyType.IsClass && destinationPropertyInfo.PropertyType.IsClass
&& !sourcePropertyInfo.PropertyType.IsArray && !destinationPropertyInfo.PropertyType.IsArray
&& !sourcePropertyInfo.PropertyType.IsGenericType && !destinationPropertyInfo.PropertyType.IsGenericType)
{
var expression = GetClassExpression(sourceProperty, sourcePropertyInfo.PropertyType, destinationPropertyInfo.PropertyType);
expressionList.Add(Expression.Assign(destinationProperty, expression));
continue;
}

//TSource派生自IEnumerable,TDestination类型为RepeatedField
//从C#IEnumerable转到Google ProtoBuffers RepeatedField
if (typeof(IEnumerable).IsAssignableFrom(sourcePropertyInfo.PropertyType)
&& destinationPropertyInfo.PropertyType.FullName.Contains(typeof(Google.Protobuf.Collections.RepeatedField<>).FullName))
{
var test = Expression.NotEqual(sourceProperty, Expression.Constant(null, sourcePropertyInfo.PropertyType));

//获取List泛型参数
var sourceArgument = sourcePropertyInfo.PropertyType.IsArray
? sourcePropertyInfo.PropertyType.GetElementType()
: sourcePropertyInfo.PropertyType.GetGenericArguments()[0];
var destinationArgument = sourcePropertyInfo.PropertyType.IsArray
? destinationPropertyInfo.PropertyType.GetElementType()
: destinationPropertyInfo.PropertyType.GetGenericArguments()[0];

//构建方法主体
var mapperType = typeof(Mapper<,>).MakeGenericType(sourceArgument, destinationArgument);
var actionType = typeof(Action<,>).MakeGenericType(sourceArgument, destinationArgument);

var methodCall = Expression.Call(mapperType.GetMethod(nameof(MapToList)
, new Type[] { sourcePropertyInfo.PropertyType, actionType })
, sourceProperty
, Expression.Constant(null, actionType));

//构建p.columns.addrange(maptolist(x.columns, null))
var repeatedFieldType = typeof(Google.Protobuf.Collections.RepeatedField<>)
.MakeGenericType(destinationArgument);

var callExpression = Expression.Call(
destinationProperty
, repeatedFieldType.GetMethod("AddRange")
, methodCall);
var exp = Expression.IfThen(test, callExpression);
expressionList.Add(exp);
continue;
}

//TSource 派生自 RepeatedField,TDestination 类型为 IEnumerable
//从 Google ProtoBuffers RepeatedField 转到 C#IEnumerable
if (sourcePropertyInfo.PropertyType.FullName.Contains(typeof(Google.Protobuf.Collections.RepeatedField<>).FullName)
&& typeof(IEnumerable).IsAssignableFrom(destinationPropertyInfo.PropertyType))
{
var test = Expression.NotEqual(sourceProperty, Expression.Constant(null, sourcePropertyInfo.PropertyType));

//获取List泛型参数
var sourceArgument = destinationPropertyInfo.PropertyType.IsArray
? sourcePropertyInfo.PropertyType.GetElementType()
: sourcePropertyInfo.PropertyType.GetGenericArguments()[0];
var destinationArgument = destinationPropertyInfo.PropertyType.IsArray
? destinationPropertyInfo.PropertyType.GetElementType()
: destinationPropertyInfo.PropertyType.GetGenericArguments()[0];

//构建方法主体
var mapperType = typeof(Mapper<,>).MakeGenericType(sourceArgument, destinationArgument);
var actionType = typeof(Action<,>).MakeGenericType(sourceArgument, destinationArgument);

var methodCall = Expression.Call(mapperType.GetMethod(nameof(MapToList)
, new Type[] { sourcePropertyInfo.PropertyType, actionType })
, sourceProperty
, Expression.Constant(null, actionType));

var result = Expression.Assign(destinationProperty, methodCall);

expressionList.Add(Expression.IfThen(test, result));
continue;
}

//TSource 和 TDestination都是直接或间接派生自IEnumerable
if (typeof(IEnumerable).IsAssignableFrom(sourcePropertyInfo.PropertyType)
&& typeof(IEnumerable).IsAssignableFrom(destinationPropertyInfo.PropertyType))
{
var expression = GetEnumerableExpression(sourceProperty, sourcePropertyInfo.PropertyType, destinationPropertyInfo.PropertyType);
expressionList.Add(Expression.Assign(destinationProperty, expression));
continue;
}
}


//可空类型转换到非可空类型,当可空类型值为null时,用默认值赋给目标属性;不为null就直接转换
else if (IsNullable(sourcePropertyInfo.PropertyType) && !IsNullable(destinationPropertyInfo.PropertyType))
{
var hasValueExpression = Expression.Equal(Expression.Property(sourceProperty, "HasValue"), Expression.Constant(true));

//C# DateTime?类型转换到ProtoBuffers TimeStamp
if (sourcePropertyInfo.PropertyType.FullName.Contains(typeof(DateTime).FullName)
&& destinationPropertyInfo.PropertyType.FullName.Contains(typeof(Google.Protobuf.WellKnownTypes.Timestamp).FullName))
{
var timestampType = typeof(Google.Protobuf.WellKnownTypes.Timestamp);

var valueExpression = Expression.Condition(hasValueExpression, Expression.Convert(sourceProperty,typeof(DateTime)), Expression.Default(typeof(DateTime)));
var callExpression = Expression.Call(valueExpression, typeof(DateTime).GetMethod("ToUniversalTime"));
var call = Expression.Call(timestampType.GetMethod("FromDateTime", new Type[] { typeof(DateTime) }), callExpression);
var conditionDateTime = Expression.Condition(hasValueExpression, call, Expression.Default(destinationPropertyInfo.PropertyType));
expressionList.Add(Expression.Assign(destinationProperty, conditionDateTime));
}
else
{
var condition = Expression.Condition(hasValueExpression, Expression.Convert(sourceProperty, destinationPropertyInfo.PropertyType), Expression.Default(destinationPropertyInfo.PropertyType));
expressionList.Add(Expression.Assign(destinationProperty, condition));
}
continue;
}

//非可空类型转换到可空类型,直接转换
else if (!IsNullable(sourcePropertyInfo.PropertyType) && IsNullable(destinationPropertyInfo.PropertyType))
{
var memberExpression = Expression.Convert(sourceProperty, destinationPropertyInfo.PropertyType);
expressionList.Add(Expression.Assign(destinationProperty, memberExpression));
continue;
}

//ProtoBuffers TimeStamp类型转换到C# DateTime类型
else if (sourcePropertyInfo.PropertyType.FullName.Contains(typeof(Google.Protobuf.WellKnownTypes.Timestamp).FullName)
&& destinationPropertyInfo.PropertyType.FullName.Contains(typeof(DateTime).FullName))
{
var timestampType = typeof(Google.Protobuf.WellKnownTypes.Timestamp);

//var callExpression = Expression.Call(timestampType, nameof(Google.Protobuf.WellKnownTypes.Timestamp.ToDateTime), new Type[] { timestampType }, sourceProperty)
var callExpression = Expression.Call(sourceProperty, timestampType.GetMethod("ToDateTime"));
expressionList.Add(Expression.Assign(destinationProperty, callExpression));
continue;
}

//C# DateTime类型转换到ProtoBuffers TimeStamp
else if (sourcePropertyInfo.PropertyType.FullName.Contains(typeof(DateTime).FullName)
&& destinationPropertyInfo.PropertyType.FullName.Contains(typeof(Google.Protobuf.WellKnownTypes.Timestamp).FullName))
{
var timestampType = typeof(Google.Protobuf.WellKnownTypes.Timestamp);
var callExpression = Expression.Call(sourceProperty, typeof(DateTime).GetMethod("ToUniversalTime"));
var call = Expression.Call(timestampType.GetMethod("FromDateTime", new Type[] { typeof(DateTime) }), callExpression);
expressionList.Add(Expression.Assign(destinationProperty, call));
continue;
}

//String 转换 Guid
else if (sourcePropertyInfo.PropertyType.FullName.Contains(typeof(string).FullName)
&& destinationPropertyInfo.PropertyType.FullName.Contains(typeof(Guid).FullName))
{
var call = Expression.Call(typeof(Guid).GetMethod("Prase", new Type[] { typeof(string) }), sourceProperty);
expressionList.Add(Expression.Assign(destinationProperty, call));
continue;
}

else if (sourcePropertyInfo.PropertyType.FullName.Contains(typeof(string).FullName)
&& destinationPropertyInfo.PropertyType.FullName.Contains(typeof(string).FullName))
{
var test = Expression.NotEqual(sourceProperty, Expression.Constant(null, sourcePropertyInfo.PropertyType));
var ifThenExp = Expression.IfThen(test, Expression.Assign(destinationProperty, sourceProperty));
expressionList.Add(ifThenExp);
continue;
}

// Enum
else if (sourcePropertyInfo.PropertyType.IsEnum && destinationPropertyInfo.PropertyType.IsEnum)
{
var convertExpression = Expression.Convert(sourceProperty, destinationPropertyInfo.PropertyType);
expressionList.Add(Expression.Assign(destinationProperty, convertExpression));
continue;
}

else if (sourcePropertyInfo.PropertyType != destinationPropertyInfo.PropertyType)
{
continue;
}
var assignExpression = Expression.Assign(destinationProperty, sourceProperty);
expressionList.Add(assignExpression);
}
catch(ArgumentNullException ex)
{
Logger.Error(ex);
throw new ArgumentNullException(ex.Message, ex);
}
catch(ArgumentException ex)
{
Logger.Error(ex);
throw new ArgumentException(ex.Message, ex);
}
catch (Exception ex)
{
Logger.Error(ex);
throw new InvalidOperationException(ex.Message, ex);
}
}

//当TDestination != null,判断TSource是否为null
var testSource = Expression.NotEqual(sourceParameter, Expression.Constant(null, sourceType));
var ifTrueSource = Expression.Block(expressionList);
var conditionSource = Expression.IfThen(testSource, ifTrueSource);

//判断TDestination是否为空
var testTarget = Expression.NotEqual(destinationParameter, Expression.Constant(null, destinationType));
var conditionTarget = Expression.IfThen(testTarget, conditionSource);

var lambda = Expression.Lambda<Action<TSource, TDestination>>(conditionTarget, sourceParameter, destinationParameter);
return lambda.Compile();
}

private static Expression GetClassExpression(Expression sourceExpression, Type sourcePropertyType, Type destinationPropertyType)
{
try
{
var test = Expression.NotEqual(sourceExpression, Expression.Constant(null, sourcePropertyType));

//构建方法主体
var mapperManagerType = typeof(Mapper<,>).MakeGenericType(sourcePropertyType, destinationPropertyType);
var actionType = typeof(Action<,>).MakeGenericType(sourcePropertyType, destinationPropertyType);

var ifTrue = Expression.Call(mapperManagerType.GetMethod(nameof(MapTo), new Type[] { sourcePropertyType, actionType }), sourceExpression, Expression.Constant(null, actionType));
var ifFalse = Expression.Constant(null, destinationPropertyType);

var condition = Expression.Condition(test, ifTrue, ifFalse);

return condition;
}
catch (Exception ex)
{
Logger.Error(ex);
throw new ArgumentException(ex.Message, ex);
}
}

private static Expression GetEnumerableExpression(Expression sourceExpression, Type sourcePropertyType, Type destinationPropertyType)
{
try
{
var test = Expression.NotEqual(sourceExpression, Expression.Constant(null, sourcePropertyType));

//获取List泛型参数
var sourceArgument = sourcePropertyType.IsArray ? sourcePropertyType.GetElementType() : sourcePropertyType.GetGenericArguments()[0];
var destinationArgument = sourcePropertyType.IsArray ? destinationPropertyType.GetElementType() : sourcePropertyType.GetGenericArguments()[0];

//构建方法主体
var mapperManagerType = typeof(Mapper<,>).MakeGenericType(sourceArgument, destinationArgument);
var actionType = typeof(Action<,>).MakeGenericType(sourceArgument, destinationArgument);

var methodCall = Expression.Call(mapperManagerType.GetMethod(nameof(MapToList), new Type[] { sourcePropertyType, actionType }), sourceExpression, Expression.Constant(null, sourcePropertyType));

Expression ifTrue;
if (destinationPropertyType.Equals(methodCall.Type))
{
ifTrue = methodCall;
}
else if (destinationPropertyType.IsArray)
{
//数组,获取泛型TSource,调用ToArray方法转化
ifTrue = Expression.Call(typeof(Enumerable), nameof(Enumerable.ToArray), new[] { methodCall.Type.GenericTypeArguments[0] }, methodCall);
}
else if (typeof(IDictionary).IsAssignableFrom(destinationPropertyType))
{
//字典,获取泛型TSource,调用ToDictionary方法转化
ifTrue = Expression.Call(typeof(Enumerable), nameof(Enumerable.ToDictionary), new[] { methodCall.Type.GenericTypeArguments[0] }, methodCall);
}
else
{
ifTrue = Expression.Convert(methodCall, destinationPropertyType);
}
var ifFlase = Expression.Constant(null, destinationPropertyType);
var conditionItem = Expression.Condition(test, ifTrue, ifFlase);

return conditionItem;
}
catch (Exception ex)
{
Logger.Error(ex);
throw new ArgumentException(ex.Message, ex);
}
}

private static bool IsNullable(Type type)
{
return type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>));
}
#endregion
}
}