我在这里看到了很多关于我的问题的文章,但没有一篇真正回答我所问的内容。我正在创建一个我可以设想的分支对象的类,就像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时需要做某些工作,那么我愿意这么做。但请举个例子。
谢谢!
为您的分支类型尝试扩展方法。这是添加重载的一种方法。也就是说,如果你的类型继承自IList。不幸的是,你无法访问受保护或私人成员。 – 2011-02-23 16:25:41