using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Reflection; using System.Runtime.Serialization; namespace WcfControlMonitorWebLib { /// /// 树结构 /// /// 树中元素的类型 [DataContract] public sealed class Tree { /// /// 构造函数 /// public Tree() { } /// /// 构造函数 /// /// 数据元素 public Tree(T data) { Data = data; } /// /// 构造函数 /// /// 包含用于标识层级关系属性的数据集合 /// 数据标识属性 /// 层级标识属性 public Tree(IEnumerable items, string keyField, string parentField) { if (items != null) { Type type = typeof(T); PropertyInfo keyProperty = type.GetProperty(keyField); PropertyInfo parentProperty = type.GetProperty(parentField); Dictionary> dic = new Dictionary>(); foreach (T item in items) { dic.Add(keyProperty.GetValue(item, null), new Tree(item)); } foreach (T item in items) { Tree node = dic[keyProperty.GetValue(item, null)]; object parentValue = parentProperty.GetValue(item, null); if (dic.ContainsKey(parentValue)) { dic[parentValue].AppendChild(node); } else { AppendChild(node); } } foreach (Tree node in GetDescendants()) { node.Level = node.Parent.Level + 1; } } } /// /// 构造函数 /// /// 包含用于标识层级关系属性的数据集合 /// 获取数据标识的方法 /// 获取数据层级标识的方法 public Tree(IEnumerable items, Func getKey, Func getParent) { if (items != null) { Dictionary> dic = new Dictionary>(); foreach (T item in items) { dic.Add(getKey(item), new Tree(item)); } foreach (T item in items) { Tree node = dic[getKey(item)]; object parentValue = getParent(item); if (dic.ContainsKey(parentValue)) { dic[parentValue].AppendChild(node); } else { AppendChild(node); } } foreach (Tree node in GetDescendants()) { node.Level = node.Parent.Level + 1; } } } /// /// 数据元素 /// [DataMember] public T Data { get; set; } /// /// 层(Root为0) /// [DataMember] public int Level { get; private set; } /// /// 双亲 /// [JsonIgnore] public Tree Parent { get; private set; } /// /// 孩子 /// [DataMember] public IList> Children { get; private set; } /// /// 添加孩子 /// /// 孩子 public void AppendChild(Tree tree) { tree.Level = Level + 1; tree.Parent = this; if (Children == null) { Children = new List>(); } Children.Add(tree); } /// /// 移除孩子 /// /// 孩子 public void RemoveChild(Tree tree) { if (Children != null && Children.Contains(tree)) { Children.Remove(tree); } } /// /// 获取祖先 /// /// 祖先 public IEnumerable> GetAncestors() { if (Parent != null) { yield return Parent; foreach (Tree ancestor in Parent.GetAncestors()) { yield return ancestor; } } } /// /// 获取指定数据元素的祖先 /// /// 数据元素 /// 祖先 public Tree GetAncestor(T data) { if (Parent == null) { return null; } else { if (Parent.Equals(data)) { return Parent; } else { return Parent.GetAncestor(data); } } } /// /// 获取后代 /// /// 后代 public IEnumerable> GetDescendants() { if (Children != null) { foreach (Tree child in Children) { yield return child; foreach (Tree descendant in child.GetDescendants()) { yield return descendant; } } } } /// /// 获取指定数据元素的后代 /// /// 数据元素 /// 后代 public Tree GetDescendant(T data) { if (Children == null) { return null; } else { foreach (Tree child in Children) { if (child.Data.Equals(data)) { return child; } else { Tree descendant = child.GetDescendant(data); if (descendant != null) { return descendant; } } } return null; } } } }