forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeProperties.cs
More file actions
386 lines (307 loc) · 13.1 KB
/
TypeProperties.cs
File metadata and controls
386 lines (307 loc) · 13.1 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
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using ServiceStack.Text;
using System.Linq.Expressions;
#if NET45 || NETSTANDARD2_0
using System.Reflection.Emit;
#endif
namespace ServiceStack
{
[Obsolete("Use TypeProperties<T>.Instance")]
public static class TypeReflector<T> { }
public class PropertyAccessor
{
public PropertyAccessor(
PropertyInfo propertyInfo,
GetMemberDelegate publicGetter,
SetMemberDelegate publicSetter)
{
PropertyInfo = propertyInfo;
PublicGetter = publicGetter;
PublicSetter = publicSetter;
}
public PropertyInfo PropertyInfo { get; }
public GetMemberDelegate PublicGetter { get; }
public SetMemberDelegate PublicSetter { get; }
}
public class TypeProperties<T> : TypeProperties
{
public static readonly TypeProperties<T> Instance = new TypeProperties<T>();
static TypeProperties()
{
Instance.Type = typeof(T);
Instance.PublicPropertyInfos = typeof(T).GetPublicProperties();
foreach (var pi in Instance.PublicPropertyInfos)
{
try
{
Instance.PropertyMap[pi.Name] = new PropertyAccessor(
pi,
PclExport.Instance.CreateGetter(pi),
PclExport.Instance.CreateSetter(pi)
);
}
catch (Exception ex)
{
Tracer.Instance.WriteError(ex);
}
}
}
public new static PropertyAccessor GetAccessor(string propertyName)
{
return Instance.PropertyMap.TryGetValue(propertyName, out PropertyAccessor info)
? info
: null;
}
}
public abstract class TypeProperties
{
static Dictionary<Type, TypeProperties> CacheMap = new Dictionary<Type, TypeProperties>();
public static Type FactoryType = typeof(TypeProperties<>);
public static TypeProperties Get(Type type)
{
if (CacheMap.TryGetValue(type, out TypeProperties value))
return value;
var genericType = FactoryType.MakeGenericType(type);
var instanceFi = genericType.GetPublicStaticField("Instance");
var instance = (TypeProperties)instanceFi.GetValue(null);
Dictionary<Type, TypeProperties> snapshot, newCache;
do
{
snapshot = CacheMap;
newCache = new Dictionary<Type, TypeProperties>(CacheMap)
{
[type] = instance
};
} while (!ReferenceEquals(
Interlocked.CompareExchange(ref CacheMap, newCache, snapshot), snapshot));
return instance;
}
public PropertyAccessor GetAccessor(string propertyName)
{
return PropertyMap.TryGetValue(propertyName, out PropertyAccessor info)
? info
: null;
}
public Type Type { get; protected set; }
public readonly Dictionary<string, PropertyAccessor> PropertyMap =
new Dictionary<string, PropertyAccessor>(PclExport.Instance.InvariantComparerIgnoreCase);
public PropertyInfo[] PublicPropertyInfos { get; protected set; }
public PropertyInfo GetPublicProperty(string name)
{
foreach (var pi in PublicPropertyInfos)
{
if (pi.Name == name)
return pi;
}
return null;
}
public GetMemberDelegate GetPublicGetter(PropertyInfo pi) => GetPublicGetter(pi?.Name);
public GetMemberDelegate GetPublicGetter(string name)
{
if (name == null)
return null;
return PropertyMap.TryGetValue(name, out PropertyAccessor info)
? info.PublicGetter
: null;
}
public SetMemberDelegate GetPublicSetter(PropertyInfo pi) => GetPublicSetter(pi?.Name);
public SetMemberDelegate GetPublicSetter(string name)
{
if (name == null)
return null;
return PropertyMap.TryGetValue(name, out PropertyAccessor info)
? info.PublicSetter
: null;
}
}
public static class PropertyInvoker
{
[Obsolete("Use CreateGetter")]
public static GetMemberDelegate GetPropertyGetterFn(this PropertyInfo propertyInfo) =>
PclExport.Instance.CreateGetter(propertyInfo);
[Obsolete("Use CreateSetter")]
public static SetMemberDelegate GetPropertySetterFn(this PropertyInfo propertyInfo) =>
PclExport.Instance.CreateSetter(propertyInfo);
public static GetMemberDelegate CreateGetter(this PropertyInfo propertyInfo) =>
PclExport.Instance.CreateGetter(propertyInfo);
public static GetMemberDelegate<T> CreateGetter<T>(this PropertyInfo propertyInfo) =>
PclExport.Instance.CreateGetter<T>(propertyInfo);
public static SetMemberDelegate CreateSetter(this PropertyInfo propertyInfo) =>
PclExport.Instance.CreateSetter(propertyInfo);
public static SetMemberDelegate<T> CreateSetter<T>(this PropertyInfo propertyInfo) =>
PclExport.Instance.CreateSetter<T>(propertyInfo);
public static GetMemberDelegate GetReflection(PropertyInfo propertyInfo) => propertyInfo.GetValue;
public static SetMemberDelegate SetReflection(PropertyInfo propertyInfo) => propertyInfo.SetValue;
public static GetMemberDelegate<T> GetExpression<T>(PropertyInfo propertyInfo)
{
var expr = GetExpressionLambda<T>(propertyInfo);
return expr.Compile();
}
public static Expression<GetMemberDelegate<T>> GetExpressionLambda<T>(PropertyInfo propertyInfo)
{
var instance = Expression.Parameter(typeof(T), "i");
var property = typeof(T) != propertyInfo.DeclaringType
? Expression.Property(Expression.TypeAs(instance, propertyInfo.DeclaringType), propertyInfo)
: Expression.Property(instance, propertyInfo);
var convertProperty = Expression.TypeAs(property, typeof(object));
return Expression.Lambda<GetMemberDelegate<T>>(convertProperty, instance);
}
public static GetMemberDelegate GetExpression(PropertyInfo propertyInfo)
{
var lambda = GetExpressionLambda(propertyInfo);
var propertyGetFn = lambda.Compile();
return propertyGetFn;
}
public static Expression<GetMemberDelegate> GetExpressionLambda(PropertyInfo propertyInfo)
{
var getMethodInfo = propertyInfo.GetGetMethod(nonPublic:true);
if (getMethodInfo == null) return null;
var oInstanceParam = Expression.Parameter(typeof(object), "oInstanceParam");
var instanceParam = Expression.Convert(oInstanceParam, propertyInfo.ReflectedType); //propertyInfo.DeclaringType doesn't work on Proxy types
var exprCallPropertyGetFn = Expression.Call(instanceParam, getMethodInfo);
var oExprCallPropertyGetFn = Expression.Convert(exprCallPropertyGetFn, typeof(object));
return Expression.Lambda<GetMemberDelegate>
(
oExprCallPropertyGetFn,
oInstanceParam
);
}
public static SetMemberDelegate<T> SetExpression<T>(PropertyInfo propertyInfo)
{
try
{
var lambda = SetExpressionLambda<T>(propertyInfo);
return lambda?.Compile();
}
catch //fallback for Android
{
var mi = propertyInfo.GetSetMethod(nonPublic: true);
return (o, convertedValue) =>
mi.Invoke(o, new[] { convertedValue });
}
}
public static Expression<SetMemberDelegate<T>> SetExpressionLambda<T>(PropertyInfo propertyInfo)
{
var mi = propertyInfo.GetSetMethod(nonPublic: true);
if (mi == null) return null;
var instance = Expression.Parameter(typeof(T), "i");
var argument = Expression.Parameter(typeof(object), "a");
var instanceType = typeof(T) != propertyInfo.DeclaringType
? (Expression)Expression.TypeAs(instance, propertyInfo.DeclaringType)
: instance;
var setterCall = Expression.Call(
instanceType,
mi,
Expression.Convert(argument, propertyInfo.PropertyType));
return Expression.Lambda<SetMemberDelegate<T>>
(
setterCall, instance, argument
);
}
public static SetMemberDelegate SetExpression(PropertyInfo propertyInfo)
{
var propertySetMethod = propertyInfo.GetSetMethod(nonPublic:true);
if (propertySetMethod == null) return null;
try
{
var instance = Expression.Parameter(typeof(object), "i");
var argument = Expression.Parameter(typeof(object), "a");
var instanceParam = Expression.Convert(instance, propertyInfo.ReflectedType);
var valueParam = Expression.Convert(argument, propertyInfo.PropertyType);
var setterCall = Expression.Call(instanceParam, propertySetMethod, valueParam);
return Expression.Lambda<SetMemberDelegate>(setterCall, instance, argument).Compile();
}
catch //fallback for Android
{
return (o, convertedValue) =>
propertySetMethod.Invoke(o, new[] { convertedValue });
}
}
#if NET45 || NETSTANDARD2_0
public static GetMemberDelegate<T> GetEmit<T>(PropertyInfo propertyInfo)
{
var getter = FieldInvoker.CreateDynamicGetMethod<T>(propertyInfo);
var gen = getter.GetILGenerator();
var mi = propertyInfo.GetGetMethod(true);
if (typeof(T).IsValueType)
{
gen.Emit(OpCodes.Ldarga_S, 0);
if (typeof(T) != propertyInfo.DeclaringType)
{
gen.Emit(OpCodes.Unbox, propertyInfo.DeclaringType);
}
}
else
{
gen.Emit(OpCodes.Ldarg_0);
if (typeof(T) != propertyInfo.DeclaringType)
{
gen.Emit(OpCodes.Castclass, propertyInfo.DeclaringType);
}
}
gen.Emit(mi.IsFinal ? OpCodes.Call : OpCodes.Callvirt, mi);
if (propertyInfo.PropertyType.IsValueType)
{
gen.Emit(OpCodes.Box, propertyInfo.PropertyType);
}
gen.Emit(OpCodes.Isinst, typeof(object));
gen.Emit(OpCodes.Ret);
return (GetMemberDelegate<T>)getter.CreateDelegate(typeof(GetMemberDelegate<T>));
}
public static GetMemberDelegate GetEmit(PropertyInfo propertyInfo)
{
var getter = FieldInvoker.CreateDynamicGetMethod(propertyInfo);
var gen = getter.GetILGenerator();
gen.Emit(OpCodes.Ldarg_0);
if (propertyInfo.DeclaringType.IsValueType)
{
gen.Emit(OpCodes.Unbox, propertyInfo.DeclaringType);
}
else
{
gen.Emit(OpCodes.Castclass, propertyInfo.DeclaringType);
}
var mi = propertyInfo.GetGetMethod(true);
gen.Emit(mi.IsFinal ? OpCodes.Call : OpCodes.Callvirt, mi);
if (propertyInfo.PropertyType.IsValueType)
{
gen.Emit(OpCodes.Box, propertyInfo.PropertyType);
}
gen.Emit(OpCodes.Ret);
return (GetMemberDelegate)getter.CreateDelegate(typeof(GetMemberDelegate));
}
public static SetMemberDelegate SetEmit(PropertyInfo propertyInfo)
{
var mi = propertyInfo.GetSetMethod(true);
if (mi == null)
return null;
var setter = FieldInvoker.CreateDynamicSetMethod(propertyInfo);
var gen = setter.GetILGenerator();
gen.Emit(OpCodes.Ldarg_0);
if (propertyInfo.DeclaringType.IsValueType)
{
gen.Emit(OpCodes.Unbox, propertyInfo.DeclaringType);
}
else
{
gen.Emit(OpCodes.Castclass, propertyInfo.DeclaringType);
}
gen.Emit(OpCodes.Ldarg_1);
if (propertyInfo.PropertyType.IsValueType)
{
gen.Emit(OpCodes.Unbox_Any, propertyInfo.PropertyType);
}
else
{
gen.Emit(OpCodes.Castclass, propertyInfo.PropertyType);
}
gen.EmitCall(mi.IsFinal ? OpCodes.Call : OpCodes.Callvirt, mi, (Type[])null);
gen.Emit(OpCodes.Ret);
return (SetMemberDelegate)setter.CreateDelegate(typeof(SetMemberDelegate));
}
#endif
}
}