答案 0 :(得分:0)
你需要有一个指向孩子父母的链接,最好只通过父母创建一个子元素:
public class Parent
{
public List<Child> Children = new List<Child>();
public void AddChild(string childArg1)
{
Children.Add(new Child(this, childArg1);
}
}
public class Child
{
public Parent Parent { get; private set; }
public Child(Parent parent, string childArg1)
{
Parent = parent;
}
}
然后你可以访问:
Parent p = childInstance.Parent;
答案 1 :(得分:0)
你可以尝试这样的事情。
public class TreeStructure
{
public TreeStructure Parent { get; set; }
public decimal Id { get; set; }
public List<TreeStructure> Childrens { get; set; } = new List<TreeStructure>();
public TreeStructure(decimal id, TreeStructure parent)
{
Id = id;
Parent = parent;
}
public object Clone()
{
return MemberwiseClone();
}
public TreeStructure GetTopParent()
{
var tree = this;
while (tree.Parent != null)
{
tree = tree.Parent;
}
return tree;
}
}