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

Commit a1acffc

Browse files
committed
C# 6 tidy up
1 parent b3920ba commit a1acffc

File tree

10 files changed

+73
-74
lines changed

10 files changed

+73
-74
lines changed

src/ServiceStack.Text/Common/DeserializeTypeRef.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ internal static Dictionary<HashedStringSegment, TypeAccessor> GetTypeAccessorMap
5353
if (isDataContract)
5454
{
5555
var dcsDataMember = propertyInfo.GetDataMember();
56-
if (dcsDataMember != null && dcsDataMember.Name != null)
56+
if (dcsDataMember?.Name != null)
5757
{
5858
propertyName = dcsDataMember.Name;
5959
}
@@ -66,16 +66,16 @@ internal static Dictionary<HashedStringSegment, TypeAccessor> GetTypeAccessorMap
6666
{
6767
foreach (var fieldInfo in fieldInfos)
6868
{
69-
var field = fieldInfo.Name;
69+
var fieldName = fieldInfo.Name;
7070
if (isDataContract)
7171
{
7272
var dcsDataMember = fieldInfo.GetDataMember();
73-
if (dcsDataMember != null && dcsDataMember.Name != null)
73+
if (dcsDataMember?.Name != null)
7474
{
75-
field = dcsDataMember.Name;
75+
fieldName = dcsDataMember.Name;
7676
}
7777
}
78-
map[new HashedStringSegment(field)] = TypeAccessor.Create(serializer, typeConfig, fieldInfo);
78+
map[new HashedStringSegment(fieldName)] = TypeAccessor.Create(serializer, typeConfig, fieldInfo);
7979
}
8080
}
8181
return map;

src/ServiceStack.Text/Common/DeserializeTypeRefJson.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ internal static object StringToType(
189189
}
190190
}
191191

192-
if (typeAccessor != null && typeAccessor.GetProperty != null && typeAccessor.SetProperty != null)
192+
if (typeAccessor?.GetProperty != null && typeAccessor.SetProperty != null)
193193
{
194194
try
195195
{
@@ -206,10 +206,10 @@ internal static object StringToType(
206206
else Tracer.Instance.WriteWarning("WARN: failed to set property {0} with: {1}", propertyName, propertyValueStr.Value);
207207
}
208208
}
209-
else if (typeConfig.OnDeserializing != null)
209+
else
210210
{
211211
// the property is not known by the DTO
212-
typeConfig.OnDeserializing(instance, propertyName.Value, propertyValueStr.Value);
212+
typeConfig.OnDeserializing?.Invoke(instance, propertyName.Value, propertyValueStr.Value);
213213
}
214214

215215
//Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);

src/ServiceStack.Text/Common/DeserializeTypeRefJsv.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ internal static object StringToType(
126126
}
127127
}
128128

129-
if (typeAccessor != null && typeAccessor.GetProperty != null && typeAccessor.SetProperty != null)
129+
if (typeAccessor?.GetProperty != null && typeAccessor.SetProperty != null)
130130
{
131131
try
132132
{
@@ -143,10 +143,10 @@ internal static object StringToType(
143143
else Tracer.Instance.WriteWarning("WARN: failed to set property {0} with: {1}", propertyName, propertyValueStr);
144144
}
145145
}
146-
else if (typeConfig.OnDeserializing != null)
146+
else
147147
{
148148
// the property is not known by the DTO
149-
typeConfig.OnDeserializing(instance, propertyName.Value, propertyValueStr);
149+
typeConfig.OnDeserializing?.Invoke(instance, propertyName.Value, propertyValueStr);
150150
}
151151

152152
//Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);

src/ServiceStack.Text/Common/JsWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public void WriteValue(TextWriter writer, object value)
337337

338338
private WriteObjectDelegate GetCoreWriteFn<T>()
339339
{
340-
if ((typeof(T).IsValueType() && !JsConfig.TreatAsRefType(typeof(T))) || JsConfig<T>.HasSerializeFn)
340+
if (typeof(T).IsValueType() && !JsConfig.TreatAsRefType(typeof(T)) || JsConfig<T>.HasSerializeFn)
341341
{
342342
return JsConfig<T>.HasSerializeFn
343343
? JsConfig<T>.WriteFn<TSerializer>

src/ServiceStack.Text/Common/WriteType.cs

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,11 @@ internal static class WriteType<T, TSerializer>
3131
internal static TypePropertyWriter[] PropertyWriters;
3232
private static readonly WriteObjectDelegate WriteTypeInfo;
3333

34-
private static bool IsIncluded
35-
{
36-
get { return (JsConfig<T>.IncludeTypeInfo.GetValueOrDefault(JsConfig.IncludeTypeInfo)); }
37-
}
34+
private static bool IsIncluded =>
35+
JsConfig<T>.IncludeTypeInfo.GetValueOrDefault(JsConfig.IncludeTypeInfo);
3836

39-
private static bool IsExcluded
40-
{
41-
get { return (JsConfig<T>.ExcludeTypeInfo.GetValueOrDefault(JsConfig.ExcludeTypeInfo)); }
42-
}
37+
private static bool IsExcluded =>
38+
JsConfig<T>.ExcludeTypeInfo.GetValueOrDefault(JsConfig.ExcludeTypeInfo);
4339

4440
static WriteType()
4541
{
@@ -264,9 +260,9 @@ internal string PropertyName
264260
{
265261
get
266262
{
267-
return (JsConfig<T>.EmitCamelCaseNames.GetValueOrDefault(JsConfig.EmitCamelCaseNames))
263+
return JsConfig<T>.EmitCamelCaseNames.GetValueOrDefault(JsConfig.EmitCamelCaseNames)
268264
? propertyNameCLSFriendly
269-
: (JsConfig<T>.EmitLowercaseUnderscoreNames.GetValueOrDefault(JsConfig.EmitLowercaseUnderscoreNames))
265+
: JsConfig<T>.EmitLowercaseUnderscoreNames.GetValueOrDefault(JsConfig.EmitLowercaseUnderscoreNames)
270266
? propertyNameLowercaseUnderscore
271267
: propertyName;
272268
}
@@ -375,18 +371,18 @@ internal static string GetPropertyName(string propertyName)
375371
: propertyName;
376372
}
377373

378-
public static void WriteProperties(TextWriter writer, object value)
374+
public static void WriteProperties(TextWriter writer, object instance)
379375
{
380-
if (value == null)
376+
if (instance == null)
381377
{
382378
writer.Write(JsWriter.EmptyMap);
383379
return;
384380
}
385381

386-
var valueType = value.GetType();
382+
var valueType = instance.GetType();
387383
if (PropertyWriters != null && valueType != typeof(T) && !typeof(T).IsAbstract())
388384
{
389-
WriteLateboundProperties(writer, value, valueType);
385+
WriteLateboundProperties(writer, instance, valueType);
390386
return;
391387
}
392388

@@ -399,7 +395,7 @@ public static void WriteProperties(TextWriter writer, object value)
399395
if (WriteTypeInfo != null || JsState.IsWritingDynamic)
400396
{
401397
if (JsConfig.PreferInterfaces && TryWriteSelfType(writer)) i++;
402-
else if (TryWriteTypeInfo(writer, value)) i++;
398+
else if (TryWriteTypeInfo(writer, instance)) i++;
403399
JsState.IsWritingDynamic = false;
404400
}
405401

@@ -410,13 +406,13 @@ public static void WriteProperties(TextWriter writer, object value)
410406
{
411407
var propertyWriter = PropertyWriters[index];
412408

413-
if (propertyWriter.shouldSerialize != null && !propertyWriter.shouldSerialize((T)value))
409+
if (propertyWriter.shouldSerialize != null && !propertyWriter.shouldSerialize((T)instance))
414410
continue;
415411

416412
var dontSkipDefault = false;
417413
if (propertyWriter.shouldSerializeDynamic != null)
418414
{
419-
var shouldSerialize = propertyWriter.shouldSerializeDynamic((T)value, propertyWriter.PropertyName);
415+
var shouldSerialize = propertyWriter.shouldSerializeDynamic((T)instance, propertyWriter.PropertyName);
420416
if (shouldSerialize.HasValue)
421417
{
422418
if (shouldSerialize.Value)
@@ -426,7 +422,7 @@ public static void WriteProperties(TextWriter writer, object value)
426422
}
427423
}
428424

429-
var propertyValue = propertyWriter.GetterFn((T)value);
425+
var propertyValue = propertyWriter.GetterFn(instance);
430426

431427
if (!dontSkipDefault)
432428
{
@@ -476,25 +472,25 @@ private static void WriteLateboundProperties(TextWriter writer, object value, Ty
476472
if (!JsConfig<T>.ExcludeTypeInfo.GetValueOrDefault()) JsState.IsWritingDynamic = false;
477473
}
478474

479-
private static readonly char[] ArrayBrackets = new[] { '[', ']' };
475+
private static readonly char[] ArrayBrackets = { '[', ']' };
480476

481-
public static void WriteComplexQueryStringProperties(string typeName, TextWriter writer, object value)
477+
public static void WriteComplexQueryStringProperties(string typeName, TextWriter writer, object instance)
482478
{
483479
var i = 0;
484480
if (PropertyWriters != null)
485481
{
486482
var len = PropertyWriters.Length;
487-
for (int index = 0; index < len; index++)
483+
for (var index = 0; index < len; index++)
488484
{
489485
var propertyWriter = PropertyWriters[index];
490-
if (propertyWriter.shouldSerialize != null && !propertyWriter.shouldSerialize((T)value)) continue;
486+
if (propertyWriter.shouldSerialize != null && !propertyWriter.shouldSerialize((T)instance)) continue;
491487

492-
var propertyValue = value != null ? propertyWriter.GetterFn((T)value) : null;
488+
var propertyValue = instance != null ? propertyWriter.GetterFn(instance) : null;
493489
if (propertyWriter.propertySuppressDefaultAttribute && Equals(propertyWriter.DefaultValue, propertyValue))
494490
continue;
495491

496492
if ((propertyValue == null
497-
|| (propertyWriter.propertySuppressDefaultConfig && Equals(propertyWriter.DefaultValue, propertyValue)))
493+
|| propertyWriter.propertySuppressDefaultConfig && Equals(propertyWriter.DefaultValue, propertyValue))
498494
&& !Serializer.IncludeNullValues)
499495
continue;
500496

@@ -504,7 +500,7 @@ public static void WriteComplexQueryStringProperties(string typeName, TextWriter
504500
if (i++ > 0)
505501
writer.Write('&');
506502

507-
var propertyValueType = propertyValue != null ? propertyValue.GetType() : null;
503+
var propertyValueType = propertyValue?.GetType();
508504
if (propertyValueType != null &&
509505
propertyValueType.IsUserType() &&
510506
!propertyValueType.HasInterface(typeof(IEnumerable)))
@@ -557,15 +553,15 @@ public static void WriteComplexQueryStringProperties(string typeName, TextWriter
557553
}
558554
}
559555

560-
public static void WriteQueryString(TextWriter writer, object value)
556+
public static void WriteQueryString(TextWriter writer, object instance)
561557
{
562558
try
563559
{
564560
JsState.QueryStringMode = true;
565561
var i = 0;
566562
foreach (var propertyWriter in PropertyWriters)
567563
{
568-
var propertyValue = propertyWriter.GetterFn((T)value);
564+
var propertyValue = propertyWriter.GetterFn(instance);
569565
if (propertyValue == null) continue;
570566

571567
if (i++ > 0)

src/ServiceStack.Text/Json/JsonWriter.Generic.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ internal static WriteObjectDelegate GetWriteFn(Type type)
4444
do
4545
{
4646
snapshot = WriteFnCache;
47-
newCache = new Dictionary<Type, WriteObjectDelegate>(WriteFnCache);
48-
newCache[type] = writeFn;
47+
newCache = new Dictionary<Type, WriteObjectDelegate>(WriteFnCache)
48+
{
49+
[type] = writeFn
50+
};
4951

5052
} while (!ReferenceEquals(
5153
Interlocked.CompareExchange(ref WriteFnCache, newCache, snapshot), snapshot));
@@ -77,8 +79,10 @@ internal static TypeInfo GetTypeInfo(Type type)
7779
do
7880
{
7981
snapshot = JsonTypeInfoCache;
80-
newCache = new Dictionary<Type, TypeInfo>(JsonTypeInfoCache);
81-
newCache[type] = writeFn;
82+
newCache = new Dictionary<Type, TypeInfo>(JsonTypeInfoCache)
83+
{
84+
[type] = writeFn
85+
};
8286

8387
} while (!ReferenceEquals(
8488
Interlocked.CompareExchange(ref JsonTypeInfoCache, newCache, snapshot), snapshot));

src/ServiceStack.Text/PclExport.Net40.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,7 +1285,8 @@ public abstract class TypeAccessor
12851285
/// <summary>
12861286
/// Does this type support new instances via a parameterless constructor?
12871287
/// </summary>
1288-
public virtual bool CreateNewSupported { get { return false; } }
1288+
public virtual bool CreateNewSupported => false;
1289+
12891290
/// <summary>
12901291
/// Create a new instance of this type
12911292
/// </summary>
@@ -1297,8 +1298,8 @@ public abstract class TypeAccessor
12971298
/// <remarks>The accessor is cached internally; a pre-existing accessor may be returned</remarks>
12981299
public static TypeAccessor Create(Type type)
12991300
{
1300-
if (type == null) throw new ArgumentNullException("type");
1301-
TypeAccessor obj = (TypeAccessor)typeLookyp[type];
1301+
if (type == null) throw new ArgumentNullException(nameof(type));
1302+
var obj = (TypeAccessor)typeLookyp[type];
13021303
if (obj != null) return obj;
13031304

13041305
lock (typeLookyp)
@@ -1449,15 +1450,16 @@ public DelegateAccessor(Func<object, string, object> getter, Action<object, stri
14491450
this.setter = setter;
14501451
this.ctor = ctor;
14511452
}
1452-
public override bool CreateNewSupported { get { return ctor != null; } }
1453+
public override bool CreateNewSupported => ctor != null;
1454+
14531455
public override object CreateNew()
14541456
{
14551457
return ctor != null ? ctor() : base.CreateNew();
14561458
}
14571459
public override object this[object target, string name]
14581460
{
1459-
get { return getter(target, name); }
1460-
set { setter(target, name, value); }
1461+
get => getter(target, name);
1462+
set => setter(target, name, value);
14611463
}
14621464
}
14631465

@@ -1474,8 +1476,8 @@ static TypeAccessor CreateNew(Type type)
14741476
// return DynamicAccessor.Singleton;
14751477
//}
14761478

1477-
PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
1478-
FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
1479+
var props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
1480+
var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
14791481
ConstructorInfo ctor = null;
14801482
if (type.IsClass && !type.IsAbstract)
14811483
{

src/ServiceStack.Text/PclExport.NetStandard.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,13 @@ public override SetMemberDelegate GetFieldSetterFn(FieldInfo fieldInfo)
328328
}
329329
#endif
330330

331+
public override SetMemberDelegate GetSetMethod(PropertyInfo propertyInfo, FieldInfo fieldInfo)
332+
{
333+
return propertyInfo.CanWrite
334+
? GetPropertySetterFn(propertyInfo)
335+
: GetFieldSetterFn(fieldInfo);
336+
}
337+
331338
public override string ToXsdDateTimeString(DateTime dateTime)
332339
{
333340
return System.Xml.XmlConvert.ToString(dateTime.ToStableUniversalTime());
@@ -473,13 +480,6 @@ public override string GetStackTrace()
473480
}
474481
#endif
475482

476-
public override SetMemberDelegate GetSetMethod(PropertyInfo propertyInfo, FieldInfo fieldInfo)
477-
{
478-
return propertyInfo.CanWrite
479-
? GetPropertySetterFn(propertyInfo)
480-
: GetFieldSetterFn(fieldInfo);
481-
}
482-
483483
public override Type UseType(Type type)
484484
{
485485
if (type.IsInterface() || type.IsAbstract())

0 commit comments

Comments
 (0)