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

Commit c56d3a6

Browse files
committed
Change to use generic GetPropertyGetterFn<T>
1 parent b317b87 commit c56d3a6

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/ServiceStack.Text/Common/WriteType.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ private static bool Init()
184184
propertyOrder,
185185
propertySuppressDefaultConfig,
186186
propertySuppressDefaultAttribute,
187-
propertyInfo.GetPropertyGetterFn(),
187+
propertyInfo.GetPropertyGetterFn<T>(),
188188
Serializer.GetWriteFn(propertyType),
189189
propertyType.GetDefaultValue(),
190190
shouldSerialize,
@@ -241,7 +241,7 @@ private static bool Init()
241241
propertyOrder,
242242
propertySuppressDefaultConfig,
243243
propertySuppressDefaultAttribute,
244-
fieldInfo.GetFieldGetterFn(),
244+
fieldInfo.GetFieldGetterFn<T>(),
245245
Serializer.GetWriteFn(propertyType),
246246
defaultValue,
247247
shouldSerialize,
@@ -276,7 +276,7 @@ internal string PropertyName
276276
internal readonly string propertyReferenceName;
277277
internal readonly string propertyNameCLSFriendly;
278278
internal readonly string propertyNameLowercaseUnderscore;
279-
internal readonly GetMemberDelegate GetterFn;
279+
internal readonly GetMemberDelegate<T> GetterFn;
280280
internal readonly WriteObjectDelegate WriteFn;
281281
internal readonly object DefaultValue;
282282
internal readonly Func<T, bool> shouldSerialize;
@@ -285,7 +285,7 @@ internal string PropertyName
285285

286286
public TypePropertyWriter(Type propertyType, string propertyName, string propertyDeclaredTypeName, string propertyNameCLSFriendly,
287287
string propertyNameLowercaseUnderscore, int propertyOrder, bool propertySuppressDefaultConfig, bool propertySuppressDefaultAttribute,
288-
GetMemberDelegate getterFn, WriteObjectDelegate writeFn, object defaultValue,
288+
GetMemberDelegate<T> getterFn, WriteObjectDelegate writeFn, object defaultValue,
289289
Func<T, bool> shouldSerialize,
290290
Func<T, string, bool?> shouldSerializeDynamic,
291291
bool isEnum)
@@ -401,18 +401,19 @@ public static void WriteProperties(TextWriter writer, object instance)
401401

402402
if (PropertyWriters != null)
403403
{
404+
var typedInstance = (T)instance;
404405
var len = PropertyWriters.Length;
405406
for (int index = 0; index < len; index++)
406407
{
407408
var propertyWriter = PropertyWriters[index];
408409

409-
if (propertyWriter.shouldSerialize != null && !propertyWriter.shouldSerialize((T)instance))
410+
if (propertyWriter.shouldSerialize != null && !propertyWriter.shouldSerialize(typedInstance))
410411
continue;
411412

412413
var dontSkipDefault = false;
413414
if (propertyWriter.shouldSerializeDynamic != null)
414415
{
415-
var shouldSerialize = propertyWriter.shouldSerializeDynamic((T)instance, propertyWriter.PropertyName);
416+
var shouldSerialize = propertyWriter.shouldSerializeDynamic(typedInstance, propertyWriter.PropertyName);
416417
if (shouldSerialize.HasValue)
417418
{
418419
if (shouldSerialize.Value)
@@ -422,7 +423,7 @@ public static void WriteProperties(TextWriter writer, object instance)
422423
}
423424
}
424425

425-
var propertyValue = propertyWriter.GetterFn(instance);
426+
var propertyValue = propertyWriter.GetterFn(typedInstance);
426427

427428
if (!dontSkipDefault)
428429
{
@@ -479,13 +480,14 @@ public static void WriteComplexQueryStringProperties(string typeName, TextWriter
479480
var i = 0;
480481
if (PropertyWriters != null)
481482
{
483+
var typedInstance = (T)instance;
482484
var len = PropertyWriters.Length;
483485
for (var index = 0; index < len; index++)
484486
{
485487
var propertyWriter = PropertyWriters[index];
486-
if (propertyWriter.shouldSerialize != null && !propertyWriter.shouldSerialize((T)instance)) continue;
488+
if (propertyWriter.shouldSerialize != null && !propertyWriter.shouldSerialize(typedInstance)) continue;
487489

488-
var propertyValue = instance != null ? propertyWriter.GetterFn(instance) : null;
490+
var propertyValue = instance != null ? propertyWriter.GetterFn(typedInstance) : null;
489491
if (propertyWriter.propertySuppressDefaultAttribute && Equals(propertyWriter.DefaultValue, propertyValue))
490492
continue;
491493

@@ -559,9 +561,10 @@ public static void WriteQueryString(TextWriter writer, object instance)
559561
{
560562
JsState.QueryStringMode = true;
561563
var i = 0;
564+
var typedInstance = (T)instance;
562565
foreach (var propertyWriter in PropertyWriters)
563566
{
564-
var propertyValue = propertyWriter.GetterFn(instance);
567+
var propertyValue = propertyWriter.GetterFn(typedInstance);
565568
if (propertyValue == null) continue;
566569

567570
if (i++ > 0)

0 commit comments

Comments
 (0)