mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 20:58:34 +01:00
shorten CopyExtensions.DeepCopyInto
This commit is contained in:
parent
cb369dec75
commit
c697d60609
1 changed files with 3 additions and 6 deletions
|
@ -58,10 +58,10 @@ namespace MLEM.Data {
|
||||||
public static void DeepCopyInto<T>(this T obj, T otherObj, BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) {
|
public static void DeepCopyInto<T>(this T obj, T otherObj, BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) {
|
||||||
foreach (var field in obj.GetType().GetFields(flags)) {
|
foreach (var field in obj.GetType().GetFields(flags)) {
|
||||||
var val = field.GetValue(obj);
|
var val = field.GetValue(obj);
|
||||||
if (field.FieldType.IsValueType) {
|
if (val == null || field.FieldType.IsValueType) {
|
||||||
// if we're a value type (struct or primitive), we can just set the value
|
// if we're a value type (struct or primitive) or null, we can just set the value
|
||||||
field.SetValue(otherObj, val);
|
field.SetValue(otherObj, val);
|
||||||
} else if (val != null) {
|
} else {
|
||||||
var otherVal = field.GetValue(otherObj);
|
var otherVal = field.GetValue(otherObj);
|
||||||
// if the object we want to copy into doesn't have a value yet, we create one
|
// if the object we want to copy into doesn't have a value yet, we create one
|
||||||
if (otherVal == null) {
|
if (otherVal == null) {
|
||||||
|
@ -69,9 +69,6 @@ namespace MLEM.Data {
|
||||||
field.SetValue(otherObj, otherVal);
|
field.SetValue(otherObj, otherVal);
|
||||||
}
|
}
|
||||||
val.DeepCopyInto(otherVal, flags);
|
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue