-
Notifications
You must be signed in to change notification settings - Fork 3
/
FastInvoke.cs
53 lines (44 loc) · 2.03 KB
/
FastInvoke.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
using System;
using System.Linq.Expressions;
using System.Reflection;
namespace DataDrain.Mapping
{
public class FastInvoke
{
public static Func<T, TReturn> BuildTypedGetter<T, TReturn>(PropertyInfo propertyInfo)
{
var reflGet = (Func<T, TReturn>) Delegate.CreateDelegate(typeof(Func<T, TReturn>), propertyInfo.GetGetMethod());
return reflGet;
}
public static Action<T, TProperty> BuildTypedSetter<T, TProperty>(PropertyInfo propertyInfo)
{
var reflSet = (Action<T, TProperty>)Delegate.CreateDelegate(typeof(Action<T, TProperty>), propertyInfo.GetSetMethod());
return reflSet;
}
public static Action<T, object> BuildUntypedSetter<T>(PropertyInfo propertyInfo)
{
var targetType = propertyInfo.DeclaringType;
var methodInfo = propertyInfo.GetSetMethod();
var exTarget = Expression.Parameter(targetType, "t");
var exValue = Expression.Parameter(typeof(object), "p");
// wir betreiben ein anObject.SetPropertyValue(object)
var exBody = Expression.Call(exTarget, methodInfo,Expression.Convert(exValue, propertyInfo.PropertyType));
var lambda = Expression.Lambda<Action<T, object>>(exBody, exTarget, exValue);
// (t, p) => t.set_StringValue(Convert(p))
var action = lambda.Compile();
return action;
}
public static Func<T, object> BuildUntypedGetter<T>(PropertyInfo propertyInfo)
{
var targetType = propertyInfo.DeclaringType;
var methodInfo = propertyInfo.GetGetMethod();
var exTarget = Expression.Parameter(targetType, "t");
var exBody = Expression.Call(exTarget, methodInfo);
var exBody2 = Expression.Convert(exBody, typeof(object));
var lambda = Expression.Lambda<Func<T, object>>(exBody2, exTarget);
// t => Convert(t.get_Foo())
var action = lambda.Compile();
return action;
}
}
}