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
160 lines (129 loc) · 5 KB
/
TypeProperties.cs
File metadata and controls
160 lines (129 loc) · 5 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
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 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,
ReflectionOptimizer.Instance.CreateGetter(pi),
ReflectionOptimizer.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
{
public static GetMemberDelegate CreateGetter(this PropertyInfo propertyInfo) =>
ReflectionOptimizer.Instance.CreateGetter(propertyInfo);
public static GetMemberDelegate<T> CreateGetter<T>(this PropertyInfo propertyInfo) =>
ReflectionOptimizer.Instance.CreateGetter<T>(propertyInfo);
public static SetMemberDelegate CreateSetter(this PropertyInfo propertyInfo) =>
ReflectionOptimizer.Instance.CreateSetter(propertyInfo);
public static SetMemberDelegate<T> CreateSetter<T>(this PropertyInfo propertyInfo) =>
ReflectionOptimizer.Instance.CreateSetter<T>(propertyInfo);
}
}