create instance of target in object-copycat when null

This commit is contained in:
2024-03-11 14:13:54 +01:00
parent 2007be9c41
commit a5edae168d
+2
View File
@@ -62,11 +62,13 @@ public class ObjectCopycat
if (property.PropertyType.IsValueType || property.PropertyType.IsEnum || property.PropertyType.Equals(STRING_TYPE) || propertyValue == null || propertyValue is IEnumerable)
{
target ??= (T)Activator.CreateInstance(type);
property.SetValue(target, propertyValue, null);
}
else if (property.PropertyType.IsClass)
{
string[] nestedExceptions = exceptions.Where(x => x.StartsWith(propertyNameWithPrefix, StringComparison.InvariantCultureIgnoreCase)).ToArray();
target ??= (T)Activator.CreateInstance(type);
property.SetValue(target, CopyPropertiesInternal(propertyValue, property.GetValue(target, null), propertyNameWithPrefix, nestedExceptions), null);
}
}