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
273 lines (217 loc) · 8.72 KB
/
TypeProperties.cs
File metadata and controls
273 lines (217 loc) · 8.72 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
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using ServiceStack.Text;
#if !NETSTANDARD1_1 || NETSTANDARD1_3
using System.Linq.Expressions;
#endif
#if NET45 || NETSTANDARD1_3
using System.Reflection.Emit;
#endif
namespace ServiceStack
{
[Obsolete("Use TypeProperties<T>.Instance")]
public static class TypeReflector<T> { }
public class TypePropertyInfo
{
public TypePropertyInfo(
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 TypePropertyInfo(
pi,
PclExport.Instance.GetPropertyGetterFn(pi),
PclExport.Instance.GetPropertySetterFn(pi)
);
}
catch (Exception ex)
{
Tracer.Instance.WriteError(ex);
}
}
}
}
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 Type Type { get; protected set; }
public readonly Dictionary<string, TypePropertyInfo> PropertyMap =
new Dictionary<string, TypePropertyInfo>(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 TypePropertyInfo 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 TypePropertyInfo info)
? info.PublicSetter
: null;
}
}
public static class PropertyInvoker
{
public static GetMemberDelegate GetPropertyGetterFn(this PropertyInfo propertyInfo) =>
PclExport.Instance.GetPropertyGetterFn(propertyInfo);
public static SetMemberDelegate GetPropertySetterFn(this PropertyInfo propertyInfo) =>
PclExport.Instance.GetPropertySetterFn(propertyInfo);
#if !SL5
public static GetMemberDelegate GetReflection(PropertyInfo propertyInfo) => propertyInfo.GetValue;
public static SetMemberDelegate SetReflection(PropertyInfo propertyInfo) => propertyInfo.SetValue;
#if !NETSTANDARD1_1 || NETSTANDARD1_3
public static GetMemberDelegate GetExpression(PropertyInfo propertyInfo)
{
var getMethodInfo = propertyInfo.GetMethodInfo();
if (getMethodInfo == null) return null;
try
{
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));
var propertyGetFn = Expression.Lambda<GetMemberDelegate>
(
oExprCallPropertyGetFn,
oInstanceParam
).Compile();
return propertyGetFn;
}
catch (Exception ex)
{
Tracer.Instance.WriteError(ex);
throw;
}
}
public static SetMemberDelegate SetExpression(PropertyInfo propertyInfo)
{
var propertySetMethod = propertyInfo.SetMethod();
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 });
}
}
#endif
#if NET45 || NETSTANDARD1_3
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);
}
gen.Emit(OpCodes.Callvirt, propertyInfo.GetGetMethod());
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 propSetMethod = propertyInfo.GetSetMethod(true);
if (propSetMethod == 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(OpCodes.Callvirt, propSetMethod, (Type[])null);
gen.Emit(OpCodes.Ret);
return (SetMemberDelegate)setter.CreateDelegate(typeof(SetMemberDelegate));
}
#endif
#endif
}
}