forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDelegateHelper.cs
97 lines (82 loc) · 3.17 KB
/
DelegateHelper.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
using System;
using System.Linq.Expressions;
using System.Reflection;
namespace NHibernate.Util
{
internal static class DelegateHelper
{
public static Func<object, T> BuildPropertyGetter<T>(System.Type type, string propertyName)
{
var parameter = Expression.Parameter(typeof (object), "x");
var instance = Expression.Convert(parameter, type);
var property = Expression.Property(instance, propertyName);
return Expression.Lambda<Func<object, T>>(property, parameter).Compile();
}
public static Action<object, T> BuildPropertySetter<T>(System.Type type, string propertyName)
{
var parameter = Expression.Parameter(typeof(object), "x");
var instance = Expression.Convert(parameter, type);
var property = Expression.Property(instance, propertyName);
var valueParameter = Expression.Parameter(typeof (T), "value");
Expression value = valueParameter;
// Cast value if required
if (!property.Type.IsAssignableFrom(typeof(T)))
{
value = Expression.Convert(valueParameter, property.Type);
}
var assign = Expression.Assign(property, value);
return Expression.Lambda<Action<object, T>>(assign, parameter, valueParameter).Compile();
}
public static Action<object> BuildAction(System.Type type, string methodName)
{
var parameter = Expression.Parameter(typeof (object));
var instance = Expression.Convert(parameter, type);
var methodCall = Expression.Call(
instance,
GetMethod(type, methodName));
return Expression.Lambda<Action<object>>(methodCall, parameter).Compile();
}
public static Action<object, T> BuildAction<T>(System.Type type, string methodName)
{
var parameter = Expression.Parameter(typeof (object), "x");
var instance = Expression.Convert(parameter, type);
var arg0 = Expression.Parameter(typeof (T), "arg0");
var methodCall = Expression.Call(
instance,
GetMethod(type, methodName, typeof (T)),
arg0);
return Expression.Lambda<Action<object, T>>(methodCall, parameter, arg0).Compile();
}
public static Action<object, T1, T2> BuildAction<T1, T2>(System.Type type, string methodName)
{
var parameter = Expression.Parameter(typeof (object), "x");
var instance = Expression.Convert(parameter, type);
var arg0 = Expression.Parameter(typeof (T1), "arg0");
var arg1 = Expression.Parameter(typeof (T2), "arg1");
var methodCall = Expression.Call(
instance,
GetMethod(type, methodName, typeof (T1), typeof (T2)),
arg0,
arg1);
return Expression.Lambda<Action<object, T1, T2>>(methodCall, parameter, arg0, arg1).Compile();
}
public static Func<object, TReturn> BuildFunc<TReturn>(System.Type type, string methodName)
{
var parameter = Expression.Parameter(typeof (object));
var instance = Expression.Convert(parameter, type);
var methodCall = Expression.Call(
instance,
GetMethod(type, methodName));
return Expression.Lambda<Func<object, TReturn>>(methodCall, parameter).Compile();
}
private static MethodInfo GetMethod(System.Type type, string methodName, params System.Type[] types)
{
return type.GetMethod(
methodName,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
null,
types,
null);
}
}
}