/// <summary> /// [0.18.0] /// </summary> public static void CopyFields <T1, T2>(object from, object to, CopyFieldMask defaultMask) where T1 : new() where T2 : new() { CopyFieldMask mask = defaultMask; foreach (CopyFieldsAttribute attr in typeof(T1).GetCustomAttributes(typeof(CopyFieldsAttribute), false)) { mask = attr.Mask; } var fields = typeof(T1).GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); foreach (var f in fields) { CopyAttribute a = new CopyAttribute(); var attributes = f.GetCustomAttributes(typeof(CopyAttribute), false); if (attributes.Length > 0) { foreach (CopyAttribute a_ in attributes) { a = a_; } } else { if ((mask & CopyFieldMask.OnlyCopyAttr) == 0 && ((mask & CopyFieldMask.SkipNotSerialized) == 0 || !f.IsNotSerialized) && ((mask & CopyFieldMask.Public) > 0 && f.IsPublic || (mask & CopyFieldMask.Serialized) > 0 && f.GetCustomAttributes(typeof(SerializeField), false).Length > 0 || (mask & CopyFieldMask.Public) == 0 && (mask & CopyFieldMask.Serialized) == 0)) { } else { continue; } } if (string.IsNullOrEmpty(a.Alias)) { a.Alias = f.Name; } var f2 = typeof(T2).GetField(a.Alias, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); if (f2 == null) { if ((mask & CopyFieldMask.Matching) == 0) { ValheimModManager.Logger.Error($"Field '{typeof(T2).Name}.{a.Alias}' not found"); } continue; } if (f.FieldType != f2.FieldType) { ValheimModManager.Logger.Error($"Fields '{typeof(T1).Name}.{f.Name}' and '{typeof(T2).Name}.{f2.Name}' have different types"); continue; } f2.SetValue(to, f.GetValue(from)); } }
public CopyFieldsAttribute(CopyFieldMask Mask) { this.Mask = Mask; }