From c697d606092965e727ab1e32a05a7708e1c58904 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 31 Jul 2020 20:26:42 +0200 Subject: [PATCH] shorten CopyExtensions.DeepCopyInto --- MLEM.Data/CopyExtensions.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/MLEM.Data/CopyExtensions.cs b/MLEM.Data/CopyExtensions.cs index 4341509..f701720 100644 --- a/MLEM.Data/CopyExtensions.cs +++ b/MLEM.Data/CopyExtensions.cs @@ -58,10 +58,10 @@ namespace MLEM.Data { public static void DeepCopyInto(this T obj, T otherObj, BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) { foreach (var field in obj.GetType().GetFields(flags)) { var val = field.GetValue(obj); - if (field.FieldType.IsValueType) { - // if we're a value type (struct or primitive), we can just set the value + if (val == null || field.FieldType.IsValueType) { + // if we're a value type (struct or primitive) or null, we can just set the value field.SetValue(otherObj, val); - } else if (val != null) { + } else { var otherVal = field.GetValue(otherObj); // if the object we want to copy into doesn't have a value yet, we create one if (otherVal == null) { @@ -69,9 +69,6 @@ namespace MLEM.Data { field.SetValue(otherObj, otherVal); } val.DeepCopyInto(otherVal, flags); - } else { - // if the value is null, we ensure that the value of the resulting object is also reset - field.SetValue(otherObj, null); } } }