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
173 lines (151 loc) · 6.25 KB
/
Copy pathStaticAccessors.cs
File metadata and controls
173 lines (151 loc) · 6.25 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
using System;
using System.Reflection;
using System.Linq;
using ServiceStack.Text;
namespace ServiceStack.Common.Reflection
{
#if MONOTOUCH || SILVERLIGHT
public static class StaticAccessors
{
}
#else
using System.Linq.Expressions;
public static class StaticAccessors
{
public static Func<object, object> GetValueGetter(Type type, PropertyInfo propertyInfo)
{
if (type != propertyInfo.DeclaringType)
{
throw new ArgumentException();
}
var instance = Expression.Parameter(typeof(object), "i");
var convertInstance = Expression.TypeAs(instance, propertyInfo.DeclaringType);
var property = Expression.Property(convertInstance, propertyInfo);
var convertProperty = Expression.TypeAs(property, typeof(object));
return Expression.Lambda<Func<object, object>>(convertProperty, instance).Compile();
}
public static Func<T, object> GetValueGetter<T>(this PropertyInfo propertyInfo)
{
if (typeof(T) != propertyInfo.DeclaringType)
{
throw new ArgumentException();
}
var instance = Expression.Parameter(propertyInfo.DeclaringType, "i");
var property = Expression.Property(instance, propertyInfo);
var convert = Expression.TypeAs(property, typeof(object));
return Expression.Lambda<Func<T, object>>(convert, instance).Compile();
}
public static Action<T, object> GetValueSetter<T>(this PropertyInfo propertyInfo)
{
if (typeof(T) != propertyInfo.DeclaringType)
{
throw new ArgumentException();
}
var instance = Expression.Parameter(propertyInfo.DeclaringType, "i");
var argument = Expression.Parameter(typeof(object), "a");
var setterCall = Expression.Call(
instance,
propertyInfo.GetSetMethod(),
Expression.Convert(argument, propertyInfo.PropertyType));
return Expression.Lambda<Action<T, object>>
(
setterCall, instance, argument
).Compile();
}
}
#endif
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 MONOTOUCH || SILVERLIGHT || 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 MONOTOUCH || SILVERLIGHT || 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);
}
}
}