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

Commit 2183e6e

Browse files
committed
Merge PublicSetterRef into TypeFieldInfo
1 parent cdb11b5 commit 2183e6e

File tree

4 files changed

+25
-31
lines changed

4 files changed

+25
-31
lines changed

src/ServiceStack.Text/AutoMappingUtils.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,9 @@ public void Populate(object to, object from,
766766
public delegate void PropertySetterDelegate(object instance, object value);
767767
public delegate object PropertyGetterDelegate(object instance);
768768

769+
public delegate void PropertySetterRefDelegate(ref object instance, object propertyValue);
770+
public delegate void PropertySetterRefGenericDelegate<T>(ref T instance, object value);
771+
769772
internal static class PropertyInvoker
770773
{
771774
public static PropertySetterDelegate GetPropertySetterFn(this PropertyInfo propertyInfo)

src/ServiceStack.Text/Common/JsDelegates.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ namespace ServiceStack.Text.Common
2828

2929
public delegate void SetPropertyDelegate(object instance, object propertyValue);
3030

31-
public delegate void SetPropertyDelegateRef(ref object instance, object propertyValue);
32-
33-
public delegate void SetPropertyDelegateRefGeneric<T>(ref T instance, object value);
34-
3531
public delegate object ParseStringDelegate(string stringValue);
3632

3733
public delegate object ConvertObjectDelegate(object fromObject);

src/ServiceStack.Text/Reflection/StaticAccessors.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public static Action<T, object> GetValueSetter<T>(this FieldInfo fieldInfo)
220220
).Compile();
221221
}
222222

223-
public static SetPropertyDelegateRefGeneric<T> GetValueSetterGenericRef<T>(this FieldInfo fieldInfo)
223+
public static PropertySetterRefGenericDelegate<T> GetValueSetterGenericRef<T>(this FieldInfo fieldInfo)
224224
{
225225
var instance = Expression.Parameter(typeof(T).MakeByRefType(), "i");
226226
var argument = Expression.Parameter(typeof(object), "a");
@@ -233,7 +233,7 @@ public static SetPropertyDelegateRefGeneric<T> GetValueSetterGenericRef<T>(this
233233
field,
234234
Expression.Convert(argument, fieldInfo.FieldType));
235235

236-
return Expression.Lambda<SetPropertyDelegateRefGeneric<T>>
236+
return Expression.Lambda<PropertySetterRefGenericDelegate<T>>
237237
(
238238
setterCall, instance, argument
239239
).Compile();

src/ServiceStack.Text/TypeFields.cs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Threading;
55
using ServiceStack.Reflection;
66
using ServiceStack.Text;
7-
using ServiceStack.Text.Common;
87

98
namespace ServiceStack
109
{
@@ -13,27 +12,28 @@ public class TypeFieldInfo
1312
public TypeFieldInfo(
1413
FieldInfo fieldInfo,
1514
PropertyGetterDelegate publicGetter,
16-
PropertySetterDelegate publicSetter)
15+
PropertySetterDelegate publicSetter,
16+
PropertySetterRefDelegate publicSetterRef)
1717
{
1818
FieldInfo = fieldInfo;
1919
PublicGetter = publicGetter;
2020
PublicSetter = publicSetter;
21+
PublicSetterRef = publicSetterRef;
2122
}
2223

2324
public FieldInfo FieldInfo { get; }
2425

2526
public PropertyGetterDelegate PublicGetter { get; }
2627

2728
public PropertySetterDelegate PublicSetter { get; }
29+
30+
public PropertySetterRefDelegate PublicSetterRef { get; }
2831
}
2932

3033
public class TypeFields<T> : TypeFields
3134
{
3235
public static readonly TypeFields<T> Instance = new TypeFields<T>();
3336

34-
public readonly Dictionary<string, SetPropertyDelegateRefGeneric<T>> GenericPublicSetters =
35-
new Dictionary<string, SetPropertyDelegateRefGeneric<T>>(PclExport.Instance.InvariantComparerIgnoreCase);
36-
3737
static TypeFields()
3838
{
3939
Instance.Type = typeof(T);
@@ -42,34 +42,24 @@ static TypeFields()
4242
{
4343
try
4444
{
45+
var fnRef = fi.GetValueSetterGenericRef<T>();
4546
Instance.FieldsMap[fi.Name] = new TypeFieldInfo(
4647
fi,
4748
PclExport.Instance.GetFieldGetterFn(fi),
48-
PclExport.Instance.GetFieldSetterFn(fi));
49-
50-
Instance.GenericPublicSetters[fi.Name] = fi.GetValueSetterGenericRef<T>();
49+
PclExport.Instance.GetFieldSetterFn(fi),
50+
delegate (ref object instance, object arg)
51+
{
52+
var valueInstance = (T)instance;
53+
fnRef(ref valueInstance, arg);
54+
instance = valueInstance;
55+
});
5156
}
5257
catch (Exception ex)
5358
{
5459
Tracer.Instance.WriteError(ex);
5560
}
5661
}
5762
}
58-
59-
public override SetPropertyDelegateRef GetPublicSetterRef(string name)
60-
{
61-
if (name == null)
62-
return null;
63-
64-
return GenericPublicSetters.TryGetValue(name, out SetPropertyDelegateRefGeneric<T> fn)
65-
? delegate (ref object instance, object arg)
66-
{
67-
var valueInstance = (T)instance;
68-
fn(ref valueInstance, arg);
69-
instance = valueInstance;
70-
}
71-
: (SetPropertyDelegateRef)null;
72-
}
7363
}
7464

7565
public abstract class TypeFields
@@ -142,9 +132,14 @@ public virtual PropertySetterDelegate GetPublicSetter(string name)
142132
: null;
143133
}
144134

145-
public virtual SetPropertyDelegateRef GetPublicSetterRef(string name)
135+
public virtual PropertySetterRefDelegate GetPublicSetterRef(string name)
146136
{
147-
throw new NotImplementedException();
137+
if (name == null)
138+
return null;
139+
140+
return FieldsMap.TryGetValue(name, out TypeFieldInfo info)
141+
? info.PublicSetterRef
142+
: null;
148143
}
149144
}
150145
}

0 commit comments

Comments
 (0)