forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeFields.cs
More file actions
182 lines (148 loc) · 5.79 KB
/
TypeFields.cs
File metadata and controls
182 lines (148 loc) · 5.79 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
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
{
public class FieldAccessor
{
public FieldAccessor(
FieldInfo fieldInfo,
GetMemberDelegate publicGetter,
SetMemberDelegate publicSetter,
SetMemberRefDelegate publicSetterRef)
{
FieldInfo = fieldInfo;
PublicGetter = publicGetter;
PublicSetter = publicSetter;
PublicSetterRef = publicSetterRef;
}
public FieldInfo FieldInfo { get; }
public GetMemberDelegate PublicGetter { get; }
public SetMemberDelegate PublicSetter { get; }
public SetMemberRefDelegate PublicSetterRef { get; }
}
public class TypeFields<T> : TypeFields
{
public static readonly TypeFields<T> Instance = new TypeFields<T>();
static TypeFields()
{
Instance.Type = typeof(T);
Instance.PublicFieldInfos = typeof(T).GetPublicFields();
foreach (var fi in Instance.PublicFieldInfos)
{
try
{
var fnRef = fi.SetExpressionRef<T>();
Instance.FieldsMap[fi.Name] = new FieldAccessor(
fi,
ReflectionOptimizer.Instance.CreateGetter(fi),
ReflectionOptimizer.Instance.CreateSetter(fi),
delegate (ref object instance, object arg)
{
var valueInstance = (T)instance;
fnRef(ref valueInstance, arg);
instance = valueInstance;
});
}
catch (Exception ex)
{
Tracer.Instance.WriteError(ex);
}
}
}
public new static FieldAccessor GetAccessor(string propertyName)
{
return Instance.FieldsMap.TryGetValue(propertyName, out FieldAccessor info)
? info
: null;
}
}
public abstract class TypeFields
{
static Dictionary<Type, TypeFields> CacheMap = new Dictionary<Type, TypeFields>();
public static Type FactoryType = typeof(TypeFields<>);
public static TypeFields Get(Type type)
{
if (CacheMap.TryGetValue(type, out TypeFields value))
return value;
var genericType = FactoryType.MakeGenericType(type);
var instanceFi = genericType.GetPublicStaticField("Instance");
var instance = (TypeFields)instanceFi.GetValue(null);
Dictionary<Type, TypeFields> snapshot, newCache;
do
{
snapshot = CacheMap;
newCache = new Dictionary<Type, TypeFields>(CacheMap)
{
[type] = instance
};
} while (!ReferenceEquals(
Interlocked.CompareExchange(ref CacheMap, newCache, snapshot), snapshot));
return instance;
}
public FieldAccessor GetAccessor(string propertyName)
{
return FieldsMap.TryGetValue(propertyName, out FieldAccessor info)
? info
: null;
}
public Type Type { get; protected set; }
public readonly Dictionary<string, FieldAccessor> FieldsMap =
new Dictionary<string, FieldAccessor>(PclExport.Instance.InvariantComparerIgnoreCase);
public FieldInfo[] PublicFieldInfos { get; protected set; }
public virtual FieldInfo GetPublicField(string name)
{
foreach (var fi in PublicFieldInfos)
{
if (fi.Name == name)
return fi;
}
return null;
}
public virtual GetMemberDelegate GetPublicGetter(FieldInfo fi) => GetPublicGetter(fi?.Name);
public virtual GetMemberDelegate GetPublicGetter(string name)
{
if (name == null)
return null;
return FieldsMap.TryGetValue(name, out FieldAccessor info)
? info.PublicGetter
: null;
}
public virtual SetMemberDelegate GetPublicSetter(FieldInfo fi) => GetPublicSetter(fi?.Name);
public virtual SetMemberDelegate GetPublicSetter(string name)
{
if (name == null)
return null;
return FieldsMap.TryGetValue(name, out FieldAccessor info)
? info.PublicSetter
: null;
}
public virtual SetMemberRefDelegate GetPublicSetterRef(string name)
{
if (name == null)
return null;
return FieldsMap.TryGetValue(name, out FieldAccessor info)
? info.PublicSetterRef
: null;
}
}
public static class FieldInvoker
{
public static GetMemberDelegate CreateGetter(this FieldInfo fieldInfo) =>
ReflectionOptimizer.Instance.CreateGetter(fieldInfo);
public static GetMemberDelegate<T> CreateGetter<T>(this FieldInfo fieldInfo) =>
ReflectionOptimizer.Instance.CreateGetter<T>(fieldInfo);
public static SetMemberDelegate CreateSetter(this FieldInfo fieldInfo) =>
ReflectionOptimizer.Instance.CreateSetter(fieldInfo);
public static SetMemberDelegate<T> CreateSetter<T>(this FieldInfo fieldInfo) =>
ReflectionOptimizer.Instance.CreateSetter<T>(fieldInfo);
public static SetMemberRefDelegate<T> SetExpressionRef<T>(this FieldInfo fieldInfo) =>
ReflectionOptimizer.Instance.CreateSetterRef<T>(fieldInfo);
}
}