private bool UpdateModified(GitObjectDb.Models.IModelObject old, GitObjectDb.Models.IModelObject @new, System.Collections.Generic.IDictionary <string, System.Collections.Generic.ISet <string> > result) { var oldKeys = new System.Collections.Generic.SortedSet <string>(); var newKeys = new System.Collections.Generic.SortedSet <string>(); ComputeKeys(old, oldKeys); ComputeKeys(@new, newKeys); var path = new Lazy <string>(() => GitObjectDb.Models.IModelObjectExtensions.GetFolderPath(old)); var anyUpdate = false; foreach (var key in System.Linq.Enumerable.Except(oldKeys, newKeys)) { if (key == null) { continue; } var set = GetIndexValues(key, result, create: false); anyUpdate |= set?.Remove(path.Value) ?? false; } foreach (var key in System.Linq.Enumerable.Except(newKeys, oldKeys)) { var set = GetIndexValues(key, result, create: true); anyUpdate |= set.Add(path.Value); } return(anyUpdate); }
private static System.Collections.Generic.ISet <string> GetIndexValues(string key, System.Collections.Generic.IDictionary <string, System.Collections.Generic.ISet <string> > dictionary, bool create) { if (!dictionary.TryGetValue(key, out var result) && create) { dictionary[key] = result = new System.Collections.Generic.SortedSet <string>(); } return(result); }
/// <summary> /// path,快速设置值。 /// </summary> /// <param name="instance">对象</param> /// <param name="path">操作路径。字典key:"aa";数组索引:[0];组合使用:"data.items[0].name"。</param> /// <param name="value">要设置的值</param> /// <returns>返回是否操作成功。</returns> public static bool Path(object instance, string path, object value) { if (instance == null) { return(false); } if (string.IsNullOrEmpty(path)) { return(false); } try { string[] paths = path.Split('.'); object parent = null; object lastTarget = instance; string lastKey = null; bool lastAllowNull = false; PathSetValueAction setMethod = null; for (int i = 0; i < paths.Length; i++) { lastAllowNull = false; string p = paths[i]; if (string.IsNullOrEmpty(p)) { return(false); } int p10 = p.IndexOf('['); string p11 = null; if (p10 > -1) { p11 = p.Substring(p10 + 1, p.Length - 2 - p10); p = p.Substring(0, p10); } //JsonObject j; if (!string.IsNullOrEmpty(p)) { if (System.Text.RegularExpressions.Regex.IsMatch(p, "^[0-9a-zA-Z]+$")) { System.Collections.Generic.IList <object> list = lastTarget as System.Collections.Generic.IList <object>; if (list != null) { parent = lastTarget; try { if (string.Equals("add", p, System.StringComparison.OrdinalIgnoreCase) || string.Equals("push", p, System.StringComparison.OrdinalIgnoreCase)) { lastTarget = null; } else { lastTarget = list[TypeExtensions.Convert <int>(p, 0)]; } } catch { lastTarget = null; } lastKey = p; lastAllowNull = true; setMethod = (p1, p2, p3, p4) => { //((System.Collections.Generic.IList<object>)p1)[TypeExtensions.Convert<int>(p3, 0)] = p4; System.Collections.Generic.IList <object> p10_list = (System.Collections.Generic.IList <object>)p1; if (string.Equals("add", p3, System.StringComparison.OrdinalIgnoreCase) || string.Equals("push", p3, System.StringComparison.OrdinalIgnoreCase)) { p10_list.Add(p4); return; } int p11_list = TypeExtensions.Convert <int>(p3, 0); if ((p10_list.Count - 1) < p11_list) { p10_list.Add(p4); return; } p10_list[p11_list] = p4; }; goto lb_Index; } } { System.Collections.Generic.IDictionary <string, object> dic = lastTarget as System.Collections.Generic.IDictionary <string, object>; if (dic != null) { parent = lastTarget; if (!dic.TryGetValue(p, out lastTarget)) { lastTarget = null; } //lastTarget = dic[p]; lastKey = p; lastAllowNull = true; setMethod = (p1, p2, p3, p4) => { //((System.Collections.Generic.IDictionary<string, object>)p1)[(string)p3] = p4; System.Collections.Generic.IDictionary <string, object> p10_list = (System.Collections.Generic.IDictionary <string, object>)p1; object p11_v; if (p10_list.TryGetValue(p3, out p11_v)) { p10_list[p3] = p4; } else { p10_list.Add(p3, p4); } }; goto lb_Index; } } { System.Collections.IDictionary dic = lastTarget as System.Collections.IDictionary; if (dic != null) { parent = lastTarget; try { lastTarget = dic[p]; } catch { lastTarget = null; } lastKey = p; lastAllowNull = true; setMethod = (p1, p2, p3, p4) => { System.Collections.IDictionary p10_list = (System.Collections.IDictionary)p1; if (p10_list.Contains(p3)) { p10_list[p3] = p4; } else { p10_list.Add(p3, p4); } //((System.Collections.IDictionary)p1)[(string)p3] = p4; }; goto lb_Index; } } { if (string.Equals(p, "length", System.StringComparison.OrdinalIgnoreCase)) { setMethod = null; lastKey = null; try { parent = lastTarget; lastTarget = TypeExtensions.Get(lastTarget, p); } catch { parent = lastTarget; lastTarget = TypeExtensions.Get(lastTarget, p == "length" ? "Length" : "length"); } } else { lastTarget = TypeExtensions.Get(lastTarget, p); lastKey = p; lastAllowNull = true; setMethod = (p1, p2, p3, p4) => TypeExtensions.Set(p1, (string)p3, p4); } goto lb_Index; } } lb_Index: if (lastTarget == null && !lastAllowNull) { return(false); } if (string.IsNullOrEmpty(p11)) { continue; } if (p11.StartsWith("\"")) { p11 = p11.Substring(1, p11.Length - 2); { System.Collections.Generic.IDictionary <string, object> dic = lastTarget as System.Collections.Generic.IDictionary <string, object>; if (dic != null) { parent = lastTarget; if (!dic.TryGetValue(p11, out lastTarget)) { lastTarget = null; } //lastTarget = dic[p11]; lastKey = p11; lastAllowNull = true; setMethod = (p1, p2, p3, p4) => { //((System.Collections.Generic.IDictionary<string, object>)p1)[(string)p3] = p4; System.Collections.Generic.IDictionary <string, object> p10_list = (System.Collections.Generic.IDictionary <string, object>)p1; object p11_v; if (p10_list.TryGetValue(p3, out p11_v)) { p10_list[p3] = p4; } else { p10_list.Add(p3, p4); } }; continue; } } { System.Collections.IDictionary dic = lastTarget as System.Collections.IDictionary; if (dic != null) { parent = lastTarget; try { lastTarget = dic[p11]; } catch { lastTarget = null; } lastKey = p11; lastAllowNull = true; setMethod = (p1, p2, p3, p4) => { System.Collections.IDictionary p10_list = (System.Collections.IDictionary)p1; if (p10_list.Contains(p3)) { p10_list[p3] = p4; } else { p10_list.Add(p3, p4); } //((System.Collections.IDictionary)p1)[(string)p3] = p4; }; continue; } } { parent = lastTarget; lastTarget = TypeExtensions.Get(lastTarget, p11); lastKey = p11; lastAllowNull = true; setMethod = (p1, p2, p3, p4) => TypeExtensions.Set(p1, (string)p3, p4); continue; } } else { { System.Collections.Generic.IList <object> list = lastTarget as System.Collections.Generic.IList <object>; if (list != null) { parent = lastTarget; try { lastTarget = list[TypeExtensions.Convert <int>(p11, 0)]; } catch { lastTarget = null; } lastKey = p11; lastAllowNull = true; setMethod = (p1, p2, p3, p4) => { //((System.Collections.Generic.IList<object>)p1)[TypeExtensions.Convert<int>(p3, 0)] = p4; System.Collections.Generic.IList <object> p10_list = (System.Collections.Generic.IList <object>)p1; if (string.Equals("add", p3, System.StringComparison.OrdinalIgnoreCase) || string.Equals("push", p3, System.StringComparison.OrdinalIgnoreCase)) { p10_list.Add(p4); return; } int p11_list = TypeExtensions.Convert <int>(p3, 0); if ((p10_list.Count - 1) < p11_list) { p10_list.Add(p4); return; } p10_list[p11_list] = p4; }; continue; } } { System.Collections.IList list = lastTarget as System.Collections.IList; if (list != null) { parent = lastTarget; try { lastTarget = list[TypeExtensions.Convert <int>(p11, 0)]; } catch { lastTarget = null; } lastKey = p11; lastAllowNull = true; setMethod = (p1, p2, p3, p4) => { System.Collections.IList p10_list = (System.Collections.IList)p1; if (string.Equals("add", p3, System.StringComparison.OrdinalIgnoreCase) || string.Equals("push", p3, System.StringComparison.OrdinalIgnoreCase)) { p10_list.Add(p4); return; } int p11_list = TypeExtensions.Convert <int>(p3, 0); if ((p10_list.Count - 1) < p11_list) { p10_list.Add(p4); return; } p10_list[p11_list] = p4; //((System.Collections.IList)p1)[TypeExtensions.Convert<int>(p3, 0)] = p4; }; continue; } } { System.Array list = lastTarget as System.Array; if (list != null) { parent = lastTarget; lastTarget = list.GetValue(TypeExtensions.Convert <int>(p11, 0)); lastKey = p11; lastAllowNull = true; setMethod = (p1, p2, p3, p4) => ((System.Array)p1).SetValue(p4, TypeExtensions.Convert <int>(p3, 0)); continue; } } } continue; } if (setMethod != null) { setMethod(parent, lastTarget, lastKey, value); return(true); } return(false); } catch { return(false); } }