2011-02-23 104 views
0

我在这里看到了很多关于我的问题的文章,但没有一篇真正回答我所问的内容。我正在创建一个我可以设想的分支对象的类,就像TreeView控件的TreeNode对象一样。每个分支可以有任何数量的分支孩子在下面(因此在上面)。这里是我的,而简单的类:如何创建一个可以有父母和子女关系的类

public class Branch { 
    public string Name { get; set; } 
    public string Link { get; set; } 
    public Branch Parent { get; private set; } 
    public List<Branch> Children { get; set; } 

    internal Branch(string Name, string Link) { 
     this.Name = Name; 
     this.Link = Link; 
     this.Children = new List<Branch>(); 
    } // Branch - Constructor - Overload 

    internal Branch(string Name, string Link, List<Branch> Children) { 
     this.Name = Name; 
     this.Link = Link; 
     this.Children = Children; 

     this.Children.ForEach(delegate(Branch branch) { 
      branch.Parent = this; 
     }); 
    } // Branch - Constructor - Overload 

    public bool HasChildren { 
     get { return this.Children.Count > 0; } 
    } // HasChildren - Property - ReadOnly 

    public string Path { 
     get { 
      string Result = ""; 

      Branch parent = this; 
      while (parent != null) { 
       Result = string.Format("{0}/{1}", parent.Name, Result); 
       parent = parent.Parent; 
      } // while stepping up the tree 

      return string.IsNullOrWhiteSpace(Result) ? "" : Result.Substring(0, Result.Length - 1); 
     } // get 
    } // Path - Property - ReadOnly 

这个伟大的工程,如果我在喜欢实例化时添加的孩子以下几点:

List<Branch> Branches = new List<Branch>() { 
    new Branch("First", "#"), 
    new Branch("Second", "#"), 
    new Branch("Third", "#", new List<Branch>() { 
     new Branch("ThirdSub1", "#"), 
     new Branch("ThirdSub2", "#") 
    }), 
    new Branch("Fourth", "#"), 
    new Branch("Fifth", "#"), 
    new Branch("Sixth", "#", new List<Branch>() { 
     new Branch("SixthSub1", "#"), 
     new Branch("SixthSub2", "#", new List<Branch>() { 
      new Branch("SixthSub2Sub1", "#"), 
      new Branch("SixthSub2Sub2", "#"), 
      new Branch("SixthSub2Sub3", "#", new List<Branch>() { 
       new Branch("Deep Deep Deep Undercover", "#"), 
      }), 
     }), 
    }), 
    new Branch("Seventh", "#"), 
    new Branch("Eighth", "#"), 
}; 

但是,如果我做到以下几点:

List<Branch> Branches = new List<Branch>(); 
Branch Test = Branches.Add(new Branch("Something", "")); 
Test.Children.Add(new Branch("Child Here", "")); 

“此处的子节点”节点没有与之关联的父节点。因此它被打破,当然Path属性不起作用。

我想我可以重写List的Add方法,但这是不允许的。处理这个问题的最好方法是什么?目前我并没有像MyBranches那样创建我自己的集合类,但是如果在实现IList或ISet或Collection时需要做某些工作,那么我愿意这么做。但请举个例子。

谢谢!

+0

为您的分支类型尝试扩展方法。这是添加重载的一种方法。也就是说,如果你的类型继承自IList。不幸的是,你无法访问受保护或私人成员。 – 2011-02-23 16:25:41

回答

4

只是人们在未来寻找这个相同的解决方案,这里是满级:

public class Branch { 
    public string Name { get; set; } 
    public string Link { get; set; } 
    public Branch Parent { get; set; } 
    public TreeBranches Children { get; private set; } 

    internal Branch(string Name, string Link) { 
     this.Name = Name; 
     this.Link = Link; 
     this.Children = new TreeBranches(this); 
    } // Branch - Constructor - Overload 

    internal Branch(string Name, string Link, TreeBranches Children) { 
     this.Name = Name; 
     this.Link = Link; 
     this.Children = Children; 

     this.Children.ToList().ForEach(delegate(Branch branch) { 
      branch.Parent = this; 
     }); 
    } // Branch - Constructor - Overload 

    /// <summary> 
    /// Returns a boolean indicating if the given Branch has any child Branches. 
    /// </summary> 
    public bool HasChildren { 
     get { return this.Children.Count > 0; } 
    } // HasChildren - Property - ReadOnly 

    /// <summary> 
    /// Gets the path from the oldest ancestor to the current Branch. 
    /// </summary> 
    public string Path { 
     get { 
      string Result = ""; 

      Branch parent = this; 
      while (parent != null) { 
       Result = string.Format("{0}/{1}", parent.Name, Result); 
       parent = parent.Parent; 
      } // while stepping up the tree 

      return string.IsNullOrWhiteSpace(Result) ? "" : Result.Substring(0, Result.Length - 1); 
     } // get 
    } // Path - Property - ReadOnly 

} // Branch - Class 

public class TreeBranches : IList<Branch> { 
    private List<Branch> branches = new List<Branch>(); 
    private Branch owner; 

    public TreeBranches() { 
     this.owner = null; 
    } 

    public TreeBranches(Branch owner) { 
     this.owner = owner; 
    } 

    public void Add(Branch branch) { 
     branch.Parent = this.owner; 
     this.branches.Add(branch); 
    } 

    #region Standard IList Method Implementation 

    IEnumerator<Branch> IEnumerable<Branch>.GetEnumerator() { return this.branches.GetEnumerator(); } 
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.branches.GetEnumerator(); } 
    public int IndexOf(Branch item) { return this.branches.IndexOf(item); } 
    public void Insert(int index, Branch item) { this.branches.Insert(index, item); } 
    public void RemoveAt(int index) { this.branches.RemoveAt(index); } 
    public Branch this[int index] { 
     get { return this.branches[index]; } 
     set { this.branches[index] = value; } 
    } 

    public void Clear() { this.branches.Clear(); } 
    public bool Contains(Branch item) { return this.branches.Contains(item); } 
    public void CopyTo(Branch[] array, int arrayIndex) { this.branches.CopyTo(array, arrayIndex); } 
    public int Count { get { return this.branches.Count(); } } 
    public bool IsReadOnly { get { return this.IsReadOnly; } } 
    public bool Remove(Branch item) { return this.branches.Remove(item); } 

    #endregion Standard IList Method Implementation 
} // TreeBranches - Class 
+0

已经照顾好了。谢谢里德! – Grandizer 2011-02-23 18:00:04

+0

虽然第二,这并没有真正回答这个问题。这只有1层深。如果我将一个子项(分支)添加到其中一个子分支对象,则其父项未设置,因为此时它正在使用无法覆盖的内部列表的添加方法。 – Grandizer 2011-02-23 18:39:37

+0

@Grandizer:它应该 - 而不是使用列表,你使用Branch中的MyBranches。通过这种方式,添加元素始终设置父级,无论级别如何发生...... – 2011-02-23 18:47:44

0

您可以从Collection<T>而不是List<T>派生,List<T>速度更快,而且对性能进行了优化,但Collection<T>更具扩展性,并允许您覆盖Add()等。

如果性能不是问题,那么使用Collection<T>,如果性能是一个问题,而不是使用Reed的示例在您的类中包含List<T>