-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathReflectionGenericHelper.cs
More file actions
498 lines (461 loc) · 21.3 KB
/
ReflectionGenericHelper.cs
File metadata and controls
498 lines (461 loc) · 21.3 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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
* 作用:通过表达式/Emit 反射获取/设置泛型属性数据。
* */
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
namespace Helper.Core.Library
{
public class ReflectionGenericHelper
{
#region 私有属性常量
public static readonly ReflectionGenericHelper Instance = new ReflectionGenericHelper();
#endregion
#region 对外公开方法
#region 创建/属性检测
/// <summary>
/// 创建一个 T 对象
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <returns></returns>
public static T New<T>() where T : class, new()
{
return new T();
}
/// <summary>
/// 创建一个 List<T> 对象
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <returns></returns>
public static IList<T> NewList<T>() where T : class
{
Type newListType = typeof(List<>);
newListType = newListType.MakeGenericType(new Type[] { typeof(T) });
return Activator.CreateInstance(newListType) as IList<T>;
}
/// <summary>
/// 判断 T 类型是否是自定义类型
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <returns></returns>
public static bool IsCustomType<T>() where T : class
{
Type type = typeof(T);
return (type != typeof(object) && Type.GetTypeCode(type) == TypeCode.Object);
}
/// <summary>
/// 判断 T 类型是否包含属性
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="propertyName">属性名称</param>
/// <returns></returns>
public static bool IsContainProperty<T>(string propertyName) where T : class
{
return typeof(T).GetProperty(propertyName) != null;
}
#endregion
#region 属性列表获取/遍历
/// <summary>
/// 属性列表遍历
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="callback">属性处理函数</param>
public static void Foreach<T>(Action<PropertyInfo> callback) where T : class
{
PropertyInfo[] propertyList = typeof(T).GetProperties();
foreach (PropertyInfo propertyInfo in propertyList)
{
callback(propertyInfo);
}
}
/// <summary>
/// 获取 T 属性名和属性值
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="t">实体类型数据</param>
/// <returns></returns>
public static Dictionary<string, object> GetPropertyDict<T>(T t) where T : class
{
Dictionary<string, object> resultDict = new Dictionary<string, object>();
Foreach<T>((PropertyInfo propertyInfo) =>
{
resultDict.Add(propertyInfo.Name, propertyInfo.GetValue(t));
});
return resultDict;
}
#endregion
#region 表达式或 Emit 反射获取设置属性值(泛型)
/// <summary>
/// T 类型索引器获取值委托
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <typeparam name="K">索引器参数类型</typeparam>
/// <typeparam name="P">索引器返回类型</typeparam>
/// <param name="reflectionType">反射类型</param>
/// <returns></returns>
public static Func<T, K, object> PropertyIndexGetCall<T, K, P>(ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class
{
if(reflectionType == ReflectionTypeEnum.Expression)
{
return ExpressionPropertyIndexGetCall<T, K, P>();
}
return EmitPropertyIndexGetCall<T, K, P>();
}
/// <summary>
/// T 类型属性获取值委托列表
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="reflectionType">反射类型</param>
/// <returns></returns>
public static Dictionary<string, Func<T, object>> PropertyGetCallDict<T>(ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class
{
if(reflectionType == ReflectionTypeEnum.Expression)
{
return ExpressionPropertyGetCallDict<T>();
}
return EmitPropertyGetCallDict<T>();
}
/// <summary>
/// T 类型属性获取值委托
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="propertyName">属性名称</param>
/// <param name="reflectionType">反射类型</param>
/// <returns></returns>
public static Func<T, object> PropertyGetCall<T>(string propertyName, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class
{
return PropertyGetCall<T>(typeof(T).GetProperty(propertyName), reflectionType);
}
/// <summary>
/// T 类型属性获取值委托
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="propertyInfo">PropertyInfo</param>
/// <param name="reflectionType">反射类型</param>
/// <returns></returns>
public static Func<T, object> PropertyGetCall<T>(PropertyInfo propertyInfo, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class
{
if(reflectionType == ReflectionTypeEnum.Expression)
{
return ExpressionPropertyGetCall<T>(propertyInfo);
}
return EmitPropertyGetCall<T>(propertyInfo);
}
/// <summary>
/// T 类型属性设置值委托列表
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="reflectionType">反射类型</param>
/// <returns></returns>
public static Dictionary<string, Action<T, object>> PropertySetCallDict<T>(ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class
{
if(reflectionType == ReflectionTypeEnum.Expression)
{
return ExpressionPropertySetCallDict<T>();
}
return EmitPropertySetCallDict<T>();
}
/// <summary>
/// T 类型属性设置值委托
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="propertyName">属性名称</param>
/// <param name="reflectionType">反射类型</param>
/// <returns></returns>
public static Action<T, object> PropertySetCall<T>(string propertyName, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class
{
return PropertySetCall<T>(typeof(T).GetProperty(propertyName), reflectionType);
}
/// <summary>
/// T 类型属性设置值委托
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="propertyInfo">PropertyInfo</param>
/// <param name="reflectionType">反射类型</param>
/// <returns></returns>
public static Action<T, object> PropertySetCall<T>(PropertyInfo propertyInfo, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class
{
if(reflectionType == ReflectionTypeEnum.Expression)
{
return ExpressionPropertySetCall<T>(propertyInfo);
}
return EmitPropertySetCall<T>(propertyInfo);
}
/// <summary>
/// 委托设置属性值
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="call">委托</param>
/// <param name="t">实体类型数据</param>
/// <param name="dataValue">要设置的数据</param>
/// <param name="propertyInfo">PropertyInfo</param>
public static void SetPropertyValue<T>(dynamic call, T t, dynamic dataValue, PropertyInfo propertyInfo) where T : class
{
DynamicSetPropertyValue(call, t, dataValue, propertyInfo);
}
#endregion
#region 表达式或 Emit 反射获取设置属性值(Type)
/// <summary>
/// 属性获取值委托列表
/// </summary>
/// <param name="type">Type</param>
/// <param name="reflectionType">反射类型</param>
/// <returns></returns>
public static dynamic PropertyGetCallDict(Type type, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression)
{
MethodInfo method = typeof(ReflectionGenericHelper).GetMethod("PropertyGetCallDict", new Type[] { typeof(ReflectionTypeEnum) });
MethodInfo generic = method.MakeGenericMethod(type);
return generic.Invoke(ReflectionGenericHelper.Instance, new object[] { reflectionType });
}
/// <summary>
/// 属性获取值委托
/// </summary>
/// <param name="type">Type</param>
/// <param name="propertyName">属性名称</param>
/// <param name="reflectionType">反射类型</param>
/// <returns></returns>
public static dynamic PropertyGetCall(Type type, string propertyName, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression)
{
return PropertyGetCall(type, type.GetProperty(propertyName), reflectionType);
}
/// <summary>
/// 属性获取值委托
/// </summary>
/// <param name="type">Type</param>
/// <param name="propertyInfo">PropertyInfo</param>
/// <param name="reflectionType">反射类型</param>
/// <returns></returns>
public static dynamic PropertyGetCall(Type type, PropertyInfo propertyInfo, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression)
{
MethodInfo method = typeof(ReflectionGenericHelper).GetMethod("PropertyGetCall", new Type[] { typeof(PropertyInfo), typeof(ReflectionTypeEnum) });
MethodInfo generic = method.MakeGenericMethod(type);
return generic.Invoke(ReflectionGenericHelper.Instance, new object[] { propertyInfo, reflectionType });
}
/// <summary>
/// 属性设置值委托列表
/// </summary>
/// <param name="type">Type</param>
/// <param name="reflectionType">反射类型</param>
/// <returns></returns>
public static dynamic PropertySetCallDict(Type type, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression)
{
MethodInfo method = typeof(ReflectionGenericHelper).GetMethod("PropertySetCallDict", new Type[] { typeof(ReflectionTypeEnum) });
MethodInfo generic = method.MakeGenericMethod(type);
return generic.Invoke(ReflectionGenericHelper.Instance, new object[] { reflectionType });
}
/// <summary>
/// 属性设置值委托
/// </summary>
/// <param name="type">Type</param>
/// <param name="propertyName">属性名称</param>
/// <param name="reflectionType">反射类型</param>
/// <returns></returns>
public static dynamic PropertySetCall(Type type, string propertyName, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression)
{
return PropertySetCall(type, type.GetProperty(propertyName), reflectionType);
}
/// <summary>
/// 属性设置值委托
/// </summary>
/// <param name="type">Type</param>
/// <param name="propertyInfo">PropertyInfo</param>
/// <param name="reflectionType">反射类型</param>
/// <returns></returns>
public static dynamic PropertySetCall(Type type, PropertyInfo propertyInfo, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression)
{
MethodInfo method = typeof(ReflectionGenericHelper).GetMethod("PropertySetCall", new Type[] { typeof(PropertyInfo), typeof(ReflectionTypeEnum) });
MethodInfo generic = method.MakeGenericMethod(type);
return generic.Invoke(ReflectionGenericHelper.Instance, new object[] { propertyInfo, reflectionType });
}
/// <summary>
/// 委托设置属性值
/// </summary>
/// <param name="call">委托</param>
/// <param name="t">数据</param>
/// <param name="dataValue">要设置的数据</param>
/// <param name="propertyInfo">PropertyInfo</param>
public static void SetPropertyValue(dynamic call, dynamic t, dynamic dataValue, PropertyInfo propertyInfo)
{
DynamicSetPropertyValue(call, t, dataValue, propertyInfo);
}
#endregion
#endregion
#region 逻辑处理私有方法
#region 表达式反射获取设置属性值
private static Func<T, K, object> ExpressionPropertyIndexGetCall<T, K, P>() where T : class
{
Type type = typeof(T);
Type paraType = typeof(K);
var instance = Expression.Parameter(type, "instance");
var parameter = Expression.Parameter(paraType);
var method = type.GetMethod("get_Item", new Type[] { paraType });
Expression expression = Expression.Call(instance, method, parameter);
return Expression.Lambda<Func<T, K, object>>(Expression.Convert(expression, typeof(object)), new ParameterExpression[] { instance, parameter }).Compile();
}
private static Dictionary<string, Func<T, object>> ExpressionPropertyGetCallDict<T>() where T : class
{
Dictionary<string, Func<T, object>> resultDict = new Dictionary<string, Func<T, object>>();
PropertyInfo[] propertyInfoList = typeof(T).GetProperties();
foreach (PropertyInfo propertyInfo in propertyInfoList)
{
resultDict.Add(propertyInfo.Name, ExpressionPropertyGetCall<T>(propertyInfo));
}
return resultDict;
}
private static Func<T, object> ExpressionPropertyGetCall<T>(PropertyInfo propertyInfo) where T : class
{
Type type = typeof(T);
var instance = Expression.Parameter(type);
var body = Expression.Convert(Expression.Property(instance, propertyInfo), typeof(object));
return Expression.Lambda<Func<T, object>>(body, instance).Compile();
}
private static Dictionary<string, Action<T, object>> ExpressionPropertySetCallDict<T>() where T : class
{
Dictionary<string, Action<T, object>> resultDict = new Dictionary<string, Action<T, object>>();
PropertyInfo[] propertyInfoList = typeof(T).GetProperties();
foreach (PropertyInfo propertyInfo in propertyInfoList)
{
resultDict.Add(propertyInfo.Name, ExpressionPropertySetCall<T>(propertyInfo));
}
return resultDict;
}
private static Action<T, object> ExpressionPropertySetCall<T>(PropertyInfo propertyInfo) where T : class
{
Type type = typeof(T);
var instance = Expression.Parameter(type, "instance");
var parmameter = Expression.Parameter(typeof(object), "parmameter");
var instanceType = Expression.Convert(instance, propertyInfo.ReflectedType);
var parmameterType = Expression.Convert(parmameter, propertyInfo.PropertyType);
var property = Expression.Property(instanceType, propertyInfo);
var body = Expression.Assign(property, parmameterType);
var lambda = Expression.Lambda<Action<T, object>>(body, instance, parmameter);
return lambda.Compile();
}
#endregion
#region Emit 反射获取/设置属性值
private static Func<T, K, object> EmitPropertyIndexGetCall<T, K, P>() where T : class
{
var type = typeof(T);
Type paraType = typeof(K);
Type returnType = typeof(P);
var dynamicMethod = new DynamicMethod("get_Item", typeof(object), new[] { type, paraType }, type);
var iLGenerator = dynamicMethod.GetILGenerator();
iLGenerator.Emit(OpCodes.Ldarg_0);
iLGenerator.Emit(OpCodes.Ldarg_1);
var indexMethod = type.GetMethod("get_Item", new Type[] { paraType });
iLGenerator.Emit(OpCodes.Callvirt, indexMethod);
if (returnType.IsValueType)
{
// 如果是值类型,装箱
iLGenerator.Emit(OpCodes.Box, returnType);
}
else
{
// 如果是引用类型,转换
iLGenerator.Emit(OpCodes.Castclass, returnType);
}
iLGenerator.Emit(OpCodes.Ret);
return dynamicMethod.CreateDelegate(typeof(Func<T, K, object>)) as Func<T, K, object>;
}
private static Dictionary<string, Func<T, object>> EmitPropertyGetCallDict<T>() where T : class
{
Dictionary<string, Func<T, object>> resultDict = new Dictionary<string, Func<T, object>>();
PropertyInfo[] propertyInfoList = typeof(T).GetProperties();
foreach (PropertyInfo propertyInfo in propertyInfoList)
{
resultDict.Add(propertyInfo.Name, EmitPropertyGetCall<T>(propertyInfo));
}
return resultDict;
}
private static Func<T, object> EmitPropertyGetCall<T>(PropertyInfo propertyInfo) where T : class
{
var type = typeof(T);
var dynamicMethod = new DynamicMethod("get_" + propertyInfo.Name, typeof(object), new[] { type }, type);
var iLGenerator = dynamicMethod.GetILGenerator();
iLGenerator.Emit(OpCodes.Ldarg_0);
iLGenerator.Emit(OpCodes.Callvirt, propertyInfo.GetMethod);
if (propertyInfo.PropertyType.IsValueType)
{
// 如果是值类型,装箱
iLGenerator.Emit(OpCodes.Box, propertyInfo.PropertyType);
}
else
{
// 如果是引用类型,转换
iLGenerator.Emit(OpCodes.Castclass, propertyInfo.PropertyType);
}
iLGenerator.Emit(OpCodes.Ret);
return dynamicMethod.CreateDelegate(typeof(Func<T, object>)) as Func<T, object>;
}
private static Dictionary<string, Action<T, object>> EmitPropertySetCallDict<T>() where T : class
{
Dictionary<string, Action<T, object>> resultDict = new Dictionary<string, Action<T, object>>();
PropertyInfo[] propertyInfoList = typeof(T).GetProperties();
foreach (PropertyInfo propertyInfo in propertyInfoList)
{
resultDict.Add(propertyInfo.Name, EmitPropertySetCall<T>(propertyInfo));
}
return resultDict;
}
private static Action<T, object> EmitPropertySetCall<T>(PropertyInfo propertyInfo) where T : class
{
var type = typeof(T);
var dynamicMethod = new DynamicMethod("EmitCallable", null, new[] { type, typeof(object) }, type.Module);
var iLGenerator = dynamicMethod.GetILGenerator();
var callMethod = type.GetMethod("set_" + propertyInfo.Name, BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Public);
var parameterInfo = callMethod.GetParameters()[0];
var local = iLGenerator.DeclareLocal(parameterInfo.ParameterType, true);
iLGenerator.Emit(OpCodes.Ldarg_1);
if (parameterInfo.ParameterType.IsValueType)
{
// 如果是值类型,拆箱
iLGenerator.Emit(OpCodes.Unbox_Any, parameterInfo.ParameterType);
}
else
{
// 如果是引用类型,转换
iLGenerator.Emit(OpCodes.Castclass, parameterInfo.ParameterType);
}
iLGenerator.Emit(OpCodes.Stloc, local);
iLGenerator.Emit(OpCodes.Ldarg_0);
iLGenerator.Emit(OpCodes.Ldloc, local);
iLGenerator.EmitCall(OpCodes.Callvirt, callMethod, null);
iLGenerator.Emit(OpCodes.Ret);
return dynamicMethod.CreateDelegate(typeof(Action<T, object>)) as Action<T, object>;
}
#endregion
private static void DynamicSetPropertyValue(dynamic call, dynamic t, dynamic dataValue, PropertyInfo propertyInfo)
{
if (dataValue == null) return;
if (dataValue.GetType() == typeof(string))
{
if (propertyInfo.PropertyType == typeof(int))
{
call(t, int.Parse(dataValue));
}
else if (propertyInfo.PropertyType == typeof(string))
{
call(t, dataValue);
}
else if (propertyInfo.PropertyType == typeof(float))
{
call(t, float.Parse(dataValue));
}
else if (propertyInfo.PropertyType == typeof(DateTime))
{
if (!string.IsNullOrEmpty(dataValue))
{
call(t, DateTime.Parse(dataValue));
}
}
}
else
{
call(t, dataValue);
}
}
#endregion
}
}