简体   繁体   English

在树结构上实现 IEnumerable

[英]Implementing IEnumerable on a tree structure

Based on the work of these guys:基于这些人的工作:

I am trying to implement a TreeView helper that would be used as such:我正在尝试实现一个 TreeView 助手,它可以这样使用:

<%= Html.TreeView("records", 
                  Library.Instance.Records, 
                  r => r.Children, 
                  r => r.ID) %>

And the tree structure is defined like this:树结构定义如下:

public class Tree<T> : TreeNode<T> where T : TreeNode<T>
{ }


public class TreeNode<T> : IDisposable where T : TreeNode<T>
{
    public T Parent { get; set; }
    public TreeNodeList<T> Children { get; set; }
}


public class TreeNodeList<T> : List<TreeNode<T>> where T : TreeNode<T>
{
    public T Parent;

    public T Add(T node)
    {
        base.Add(node);
        node.Parent = (T)Parent;
        return node;
    }

    public void Remove(T node)
    {
        if (node != null)
            node.Parent = null;
        base.Remove(node);
    }
}

And the TreeView helper has this signature: TreeView 助手具有以下签名:

public static string TreeView<T>(this HtmlHelper htmlHelper, string treeId,
   IEnumerable<T> rootItems, Func<T, IEnumerable<T>> childrenProperty, 
   Func<T, string> itemContent, bool includeJavascript, string emptyContent)
{
    ...
}   

So as a result i need my Tree struture to implement IEnumerable so i can use it with the TreeView helper, and this leads to the question: where and how would i implement IEnumerable in this situation?因此,我需要我的 Tree 结构来实现 IEnumerable,以便我可以将它与 TreeView 助手一起使用,这导致了一个问题:在这种情况下,我将在哪里以及如何实现 IEnumerable?

This article by Wes Dyer is good reading for anyone who wants to implement a recursive tree iterator: Wes Dyer 的这篇文章适合任何想要实现递归树迭代器的人阅读:

https://docs.microsoft.com/en-us/archive/blogs/wesdyer/all-about-iterators https://docs.microsoft.com/en-us/archive/blogs/wesdyer/all-about-iterators

I don't fully understand the exact details of your tree structure, but here is a simple implementation that takes a generic tree of nodes and recursively renders it into html lists.我不完全了解您的树结构的确切细节,但这里有一个简单的实现,它采用节点的通用树并递归地将其呈现为 html 列表。

public static string TreeView<T>(IEnumerable<T> rootItems,
                                 Func<T, IEnumerable<T>> childrenProperty,
                                 Func<T, string> itemContent)
{
    if (rootItems == null || !rootItems.Any()) return null;

    var builder = new StringBuilder();
    builder.AppendLine("<ul>");

    foreach (var item in rootItems)
    {
        builder.Append("  <li>").Append(itemContent(item)).AppendLine("</li>");
        var childContent = htmlHelper.TreeView(treeId,
                                               childrenProperty(item),
                                               childrenProperty,
                                               itemContent);

        if (childContent != null)
        {
            var indented = childContent.Replace(Environment.NewLine,
                                                Environment.NewLine + "  ");
            builder.Append("  ").AppendLine(indented);
        }
    }

    builder.Append("</ul>");
    return builder.ToString();
}

The node class that I'm using is relatively simple with only two properties.我使用的节点类相对简单,只有两个属性。

public class Node<T>
{
    public Node(T data)
    {
        Data = data;
        Children = new List<Node<T>>();
    }

    public T Data { get; private set; }
    public ICollection<Node<T>> Children { get; private set; }
}

Here is some test code that outputs the tree to the console.这是一些将树输出到控制台的测试代码。

var Records = new[] {
    new Node<string>("one") {
        Children = {
            new Node<string>("one-one") {
                Children = {
                    new Node<string>("one-one-one"),
                    new Node<string>("one-one-two"),
                    new Node<string>("one-one-three")
                }
            },
            new Node<string>("one-two"),
            new Node<string>("one-three")
        }
    },
    new Node<string>("two"),
    new Node<string>("three")
};
Console.WriteLine(TreeView(Records,
                           r => r.Children,
                           r => r.Data));

And here are the results from the above code.以下是上述代码的结果。

<ul>
  <li>one</li>
  <ul>
    <li>one-one</li>
    <ul>
      <li>one-one-one</li>
      <li>one-one-two</li>
      <li>one-one-three</li>
    </ul>
    <li>one-two</li>
    <li>one-three</li>
  </ul>
  <li>two</li>
  <li>three</li>
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM