diff --git a/SourceGen/MainController.cs b/SourceGen/MainController.cs index 284955f..433d694 100644 --- a/SourceGen/MainController.cs +++ b/SourceGen/MainController.cs @@ -462,7 +462,7 @@ namespace SourceGen { if (!string.IsNullOrEmpty(pseudoCereal)) { PseudoOp.PseudoOpNames deser = PseudoOp.PseudoOpNames.Deserialize(pseudoCereal); if (deser != null) { - mPseudoOpNames.Merge(deser); + mPseudoOpNames = PseudoOp.PseudoOpNames.Merge(mPseudoOpNames, deser); } } diff --git a/SourceGen/PseudoOp.cs b/SourceGen/PseudoOp.cs index a1626f2..116081e 100644 --- a/SourceGen/PseudoOp.cs +++ b/SourceGen/PseudoOp.cs @@ -165,19 +165,31 @@ namespace SourceGen { } /// - /// Merges the non-null, non-empty strings in "other" into this instance. + /// Merges the non-null, non-empty strings. /// - public void Merge(PseudoOpNames other) { - // Lots of fields, we don't do this often... use reflection. - Type type = GetType(); - PropertyInfo[] props = type.GetProperties(); - foreach (PropertyInfo pi in props) { - string str = (string)pi.GetValue(other); - if (string.IsNullOrEmpty(str)) { + public static PseudoOpNames Merge(PseudoOpNames basePon, PseudoOpNames newPon) { + Dictionary baseDict = PropsToDict(basePon); + Dictionary newDict = PropsToDict(newPon); + + foreach (KeyValuePair kvp in newDict) { + if (string.IsNullOrEmpty(kvp.Value)) { continue; } - pi.SetValue(this, str); + baseDict[kvp.Key] = kvp.Value; } + + return new PseudoOpNames(baseDict); + } + + private static Dictionary PropsToDict(PseudoOpNames pon) { + Dictionary dict = new Dictionary(); + foreach (PropertyInfo prop in pon.GetType().GetProperties()) { + string value = (string)prop.GetValue(pon); + if (!string.IsNullOrEmpty(value)) { + dict[prop.Name] = value; + } + } + return dict; } public string Serialize() { @@ -185,15 +197,7 @@ namespace SourceGen { // which means a lot of double-quote escaping. We could do something here // that stored more nicely but it doesn't seem worth the effort. JavaScriptSerializer ser = new JavaScriptSerializer(); - - Dictionary dict = new Dictionary(); - foreach (PropertyInfo prop in GetType().GetProperties()) { - string value = (string)prop.GetValue(this); - if (!string.IsNullOrEmpty(value)) { - dict[prop.Name] = value; - } - } - + Dictionary dict = PropsToDict(this); return ser.Serialize(dict); }