Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit cdb11b5

Browse files
committed
Maintain a single dictionary for TypeFields + TypeProperties
1 parent 18bb0b3 commit cdb11b5

File tree

2 files changed

+65
-67
lines changed

2 files changed

+65
-67
lines changed

src/ServiceStack.Text/TypeFields.cs

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@
88

99
namespace ServiceStack
1010
{
11+
public class TypeFieldInfo
12+
{
13+
public TypeFieldInfo(
14+
FieldInfo fieldInfo,
15+
PropertyGetterDelegate publicGetter,
16+
PropertySetterDelegate publicSetter)
17+
{
18+
FieldInfo = fieldInfo;
19+
PublicGetter = publicGetter;
20+
PublicSetter = publicSetter;
21+
}
22+
23+
public FieldInfo FieldInfo { get; }
24+
25+
public PropertyGetterDelegate PublicGetter { get; }
26+
27+
public PropertySetterDelegate PublicSetter { get; }
28+
}
29+
1130
public class TypeFields<T> : TypeFields
1231
{
1332
public static readonly TypeFields<T> Instance = new TypeFields<T>();
@@ -23,10 +42,12 @@ static TypeFields()
2342
{
2443
try
2544
{
26-
Instance.PublicGetters[fi.Name] = PclExport.Instance.GetFieldGetterFn(fi);
27-
Instance.PublicSetters[fi.Name] = fi.GetValueSetter(typeof(T));
45+
Instance.FieldsMap[fi.Name] = new TypeFieldInfo(
46+
fi,
47+
PclExport.Instance.GetFieldGetterFn(fi),
48+
PclExport.Instance.GetFieldSetterFn(fi));
49+
2850
Instance.GenericPublicSetters[fi.Name] = fi.GetValueSetterGenericRef<T>();
29-
Instance.PublicFields[fi.Name] = fi;
3051
}
3152
catch (Exception ex)
3253
{
@@ -82,14 +103,8 @@ public static TypeFields Get(Type type)
82103

83104
public Type Type { get; protected set; }
84105

85-
public readonly Dictionary<string, PropertyGetterDelegate> PublicGetters =
86-
new Dictionary<string, PropertyGetterDelegate>(PclExport.Instance.InvariantComparerIgnoreCase);
87-
88-
public readonly Dictionary<string, Action<object, object>> PublicSetters =
89-
new Dictionary<string, Action<object, object>>(PclExport.Instance.InvariantComparerIgnoreCase);
90-
91-
public readonly Dictionary<string, FieldInfo> PublicFields =
92-
new Dictionary<string, FieldInfo>(PclExport.Instance.InvariantComparerIgnoreCase);
106+
public readonly Dictionary<string, TypeFieldInfo> FieldsMap =
107+
new Dictionary<string, TypeFieldInfo>(PclExport.Instance.InvariantComparerIgnoreCase);
93108

94109
public FieldInfo[] PublicFieldInfos { get; protected set; }
95110

@@ -103,43 +118,27 @@ public virtual FieldInfo GetPublicField(string name)
103118
return null;
104119
}
105120

106-
public virtual PropertyGetterDelegate GetPublicGetter(FieldInfo fi)
107-
{
108-
if (fi == null)
109-
return null;
110-
111-
return PublicGetters.TryGetValue(fi.Name, out PropertyGetterDelegate fn)
112-
? fn
113-
: PclExport.Instance.GetFieldGetterFn(fi);
114-
}
121+
public virtual PropertyGetterDelegate GetPublicGetter(FieldInfo fi) => GetPublicGetter(fi?.Name);
115122

116123
public virtual PropertyGetterDelegate GetPublicGetter(string name)
117124
{
118125
if (name == null)
119126
return null;
120127

121-
return PublicGetters.TryGetValue(name, out PropertyGetterDelegate fn)
122-
? fn
128+
return FieldsMap.TryGetValue(name, out TypeFieldInfo info)
129+
? info.PublicGetter
123130
: null;
124131
}
125132

126-
public virtual Action<object, object> GetPublicSetter(FieldInfo fi)
127-
{
128-
if (fi == null)
129-
return null;
130-
131-
return PublicSetters.TryGetValue(fi.Name, out Action<object, object> fn)
132-
? fn
133-
: fi.GetValueSetter(Type);
134-
}
133+
public virtual PropertySetterDelegate GetPublicSetter(FieldInfo fi) => GetPublicSetter(fi?.Name);
135134

136-
public virtual Action<object, object> GetPublicSetter(string name)
135+
public virtual PropertySetterDelegate GetPublicSetter(string name)
137136
{
138137
if (name == null)
139138
return null;
140139

141-
return PublicSetters.TryGetValue(name, out Action<object, object> fn)
142-
? fn
140+
return FieldsMap.TryGetValue(name, out TypeFieldInfo info)
141+
? info.PublicSetter
143142
: null;
144143
}
145144

src/ServiceStack.Text/TypeProperties.cs

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,25 @@ namespace ServiceStack
1010
[Obsolete("Use TypeProperties<T>.Instance")]
1111
public static class TypeReflector<T> { }
1212

13+
public class TypePropertyInfo
14+
{
15+
public TypePropertyInfo(
16+
PropertyInfo propertyInfo,
17+
Func<object, object> publicGetter,
18+
Action<object, object> publicSetter)
19+
{
20+
PropertyInfo = propertyInfo;
21+
PublicGetter = publicGetter;
22+
PublicSetter = publicSetter;
23+
}
24+
25+
public PropertyInfo PropertyInfo { get; }
26+
27+
public Func<object, object> PublicGetter { get; }
28+
29+
public Action<object, object> PublicSetter { get; }
30+
}
31+
1332
public class TypeProperties<T> : TypeProperties
1433
{
1534
public static readonly TypeProperties<T> Instance = new TypeProperties<T>();
@@ -22,9 +41,11 @@ static TypeProperties()
2241
{
2342
try
2443
{
25-
Instance.PublicGetters[pi.Name] = pi.GetValueGetter(typeof(T));
26-
Instance.PublicSetters[pi.Name] = pi.GetValueSetter(typeof(T));
27-
Instance.PublicProperties[pi.Name] = pi;
44+
Instance.PropertyMap[pi.Name] = new TypePropertyInfo(
45+
pi,
46+
pi.GetValueGetter(typeof(T)),
47+
pi.GetValueSetter(typeof(T))
48+
);
2849
}
2950
catch (Exception ex)
3051
{
@@ -65,14 +86,8 @@ public static TypeProperties Get(Type type)
6586

6687
public Type Type { get; protected set; }
6788

68-
public readonly Dictionary<string, Func<object, object>> PublicGetters =
69-
new Dictionary<string, Func<object, object>>(PclExport.Instance.InvariantComparerIgnoreCase);
70-
71-
public readonly Dictionary<string, Action<object, object>> PublicSetters =
72-
new Dictionary<string, Action<object, object>>(PclExport.Instance.InvariantComparerIgnoreCase);
73-
74-
public readonly Dictionary<string, PropertyInfo> PublicProperties =
75-
new Dictionary<string, PropertyInfo>(PclExport.Instance.InvariantComparerIgnoreCase);
89+
public readonly Dictionary<string, TypePropertyInfo> PropertyMap =
90+
new Dictionary<string, TypePropertyInfo>(PclExport.Instance.InvariantComparerIgnoreCase);
7691

7792
public PropertyInfo[] PublicPropertyInfos { get; protected set; }
7893

@@ -86,43 +101,27 @@ public PropertyInfo GetPublicProperty(string name)
86101
return null;
87102
}
88103

89-
public Func<object, object> GetPublicGetter(PropertyInfo pi)
90-
{
91-
if (pi == null)
92-
return null;
93-
94-
return PublicGetters.TryGetValue(pi.Name, out Func<object, object> fn)
95-
? fn
96-
: pi.GetValueGetter();
97-
}
104+
public Func<object, object> GetPublicGetter(PropertyInfo pi) => GetPublicGetter(pi?.Name);
98105

99106
public Func<object, object> GetPublicGetter(string name)
100107
{
101108
if (name == null)
102109
return null;
103110

104-
return PublicGetters.TryGetValue(name, out Func<object, object> fn)
105-
? fn
111+
return PropertyMap.TryGetValue(name, out TypePropertyInfo info)
112+
? info.PublicGetter
106113
: null;
107114
}
108115

109-
public Action<object, object> GetPublicSetter(PropertyInfo pi)
110-
{
111-
if (pi == null)
112-
return null;
113-
114-
return PublicSetters.TryGetValue(pi.Name, out Action<object, object> fn)
115-
? fn
116-
: pi.GetValueSetter();
117-
}
116+
public Action<object, object> GetPublicSetter(PropertyInfo pi) => GetPublicSetter(pi?.Name);
118117

119118
public Action<object, object> GetPublicSetter(string name)
120119
{
121120
if (name == null)
122121
return null;
123122

124-
return PublicSetters.TryGetValue(name, out Action<object, object> fn)
125-
? fn
123+
return PropertyMap.TryGetValue(name, out TypePropertyInfo info)
124+
? info.PublicSetter
126125
: null;
127126
}
128127
}

0 commit comments

Comments
 (0)