forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticAccessors.cs
More file actions
112 lines (100 loc) · 4.11 KB
/
StaticAccessors.cs
File metadata and controls
112 lines (100 loc) · 4.11 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
using System;
using System.Reflection;
using System.Linq.Expressions;
namespace ServiceStack.Reflection
{
public static class StaticAccessors<TEntity>
{
/// <summary>
/// Func to get the Strongly-typed field
/// </summary>
public static Func<TEntity, TId> TypedGetPropertyFn<TId>(PropertyInfo pi)
{
var mi = pi.GetMethodInfo();
return (Func<TEntity, TId>)mi.MakeDelegate(typeof(Func<TEntity, TId>));
}
/// <summary>
/// Required to cast the return ValueType to an object for caching
/// </summary>
public static Func<TEntity, object> ValueUnTypedGetPropertyFn<TId>(PropertyInfo pi)
{
var typedPropertyFn = TypedGetPropertyFn<TId>(pi);
return x => typedPropertyFn(x);
}
public static Func<TEntity, object> ValueUnTypedGetPropertyTypeFn(PropertyInfo pi)
{
var mi = typeof(StaticAccessors<TEntity>).GetMethodInfo("TypedGetPropertyFn");
var genericMi = mi.MakeGenericMethod(pi.PropertyType);
var typedGetPropertyFn = (Delegate)genericMi.Invoke(null, new[] { pi });
#if IOS || SL5 || NETFX_CORE
return x => typedGetPropertyFn.InvokeMethod(x);
#else
var typedMi = typedGetPropertyFn.Method;
var paramFunc = Expression.Parameter(typeof(object), "oFunc");
var expr = Expression.Lambda<Func<TEntity, object>>(
Expression.Convert(
Expression.Call(
Expression.Convert(paramFunc, typedMi.DeclaringType),
typedMi
),
typeof(object)
),
paramFunc
);
return expr.Compile();
#endif
}
public static Func<object, object> UnTypedGetPropertyFn<TId>(PropertyInfo pi)
{
var typedPropertyFn = TypedGetPropertyFn<TId>(pi);
return x => typedPropertyFn((TEntity)x);
}
/// <summary>
/// Func to set the Strongly-typed field
/// </summary>
public static Action<TEntity, TId> TypedSetPropertyFn<TId>(PropertyInfo pi)
{
var mi = pi.SetMethod();
return (Action<TEntity, TId>)mi.MakeDelegate(typeof(Action<TEntity, TId>));
}
/// <summary>
/// Required to cast the ValueType to an object for caching
/// </summary>
public static Action<TEntity, object> ValueUnTypedSetPropertyFn<TId>(PropertyInfo pi)
{
var typedPropertyFn = TypedSetPropertyFn<TId>(pi);
return (x, y) => typedPropertyFn(x, (TId)y);
}
public static Action<TEntity, object> ValueUnTypedSetPropertyTypeFn(PropertyInfo pi)
{
var mi = typeof(StaticAccessors<TEntity>).GetMethodInfo("TypedSetPropertyFn");
var genericMi = mi.MakeGenericMethod(pi.PropertyType);
var typedSetPropertyFn = (Delegate)genericMi.Invoke(null, new[] { pi });
#if IOS || SL5 || NETFX_CORE
return (x, y) => typedSetPropertyFn.InvokeMethod(x, new[] { y });
#else
var typedMi = typedSetPropertyFn.Method;
var paramFunc = Expression.Parameter(typeof(object), "oFunc");
var paramValue = Expression.Parameter(typeof(object), "oValue");
var expr = Expression.Lambda<Action<TEntity, object>>(
Expression.Call(
Expression.Convert(paramFunc, typedMi.DeclaringType),
typedMi,
Expression.Convert(paramValue, pi.PropertyType)
),
paramFunc,
paramValue
);
return expr.Compile();
#endif
}
/// <summary>
/// Required to cast the ValueType to an object for caching
/// </summary>
public static Action<object, object> UnTypedSetPropertyFn<TId>(PropertyInfo pi)
{
var typedPropertyFn = TypedSetPropertyFn<TId>(pi);
return (x, y) => typedPropertyFn((TEntity)x, (TId)y);
}
}
}