【问题标题】:IEnumerable and Recursion using yield returnIEnumerable 和 Recursion 使用 yield return
【发布时间】:2026-01-21 09:25:01
【问题描述】:

我有一个 IEnumerable<T> 方法,用于在 WebForms 页面中查找控件。

该方法是递归的,当yield return 返回递归调用的值时,我在返回我想要的类型时遇到了一些问题。

我的代码如下:

public static IEnumerable<Control> GetDeepControlsByType<T>(this Control control)
{
    foreach(Control c in control.Controls)
    {
        if (c is T)
        {
            yield return c;
        }

        if(c.Controls.Count > 0)
        {
            yield return c.GetDeepControlsByType<T>();
        }
    }
}

这当前会引发“无法转换表达式类型”错误。但是,如果此方法返回类型 IEnumerable&lt;Object&gt;,则代码会构建,但输出中会返回错误的类型。

有没有办法在使用yield return 的同时也使用递归?

【问题讨论】:

  • *.com/questions/1815497/… :链接到线程“枚举不是固有 IEnumerable 的集合?”上的“mrydengrens”答案他的示例代码基于 Eric Lippert 的一篇博客文章,该文章向您展示了如何在 Linq 的递归枚举中使用堆栈,从而避免迭代器可能使用昂贵的内存。恕我直言非常有用!
  • 顺便说一句。 if(c.Controls.Count &gt; 0) --> if(c.Controls.Any()),特别是如果你也在屈服:)
  • 我认为这种情况不会从屈服中受益。为了完整起见,我提供了一个没有yield 的实现。请看下面:)而且它也是单行的:)
  • 你应该小心避免在递归函数中使用yield return,内存使用量会爆炸式增长。见*.com/a/30300257/284795

标签: c# generics ienumerable yield


【解决方案1】:

虽然那里有很多好的答案,但我仍然要补充一点,可以使用 LINQ 方法来完成同样的事情。

例如,OP的原始代码可以重写为:

public static IEnumerable<Control> 
                           GetDeepControlsByType<T>(this Control control)
{
   return control.Controls.OfType<T>()
          .Union(control.Controls.SelectMany(c => c.GetDeepControlsByType<T>()));        
}

【讨论】:

  • 三年前发布了使用相同方法的解决方案。
  • @Servy 虽然它是相似的(顺便说一句,我在所有答案之间都错过了......在写这个答案时),它仍然不同,因为它使用 .OfType 过滤和 .Union ()
  • OfType 并不是真正意义上的不同。最多只是一个小的风格变化。一个控件不能是多个控件的子控件,因此遍历的树已经不完整。使用Union 而不是Concat 是不必要地验证已经保证唯一的序列的唯一性,因此是客观的降级。
【解决方案2】:

正如 Jon Skeet 和上校 Panic 在他们的回答中指出的那样,如果树非常深,在递归方法中使用 yield return 可能会导致性能问题。

这是一个通用的非递归扩展方法,它执行深度优先遍历一系列树:

public static IEnumerable<TSource> RecursiveSelect<TSource>(
    this IEnumerable<TSource> source, Func<TSource, IEnumerable<TSource>> childSelector)
{
    var stack = new Stack<IEnumerator<TSource>>();
    var enumerator = source.GetEnumerator();

    try
    {
        while (true)
        {
            if (enumerator.MoveNext())
            {
                TSource element = enumerator.Current;
                yield return element;

                stack.Push(enumerator);
                enumerator = childSelector(element).GetEnumerator();
            }
            else if (stack.Count > 0)
            {
                enumerator.Dispose();
                enumerator = stack.Pop();
            }
            else
            {
                yield break;
            }
        }
    }
    finally
    {
        enumerator.Dispose();

        while (stack.Count > 0) // Clean up in case of an exception.
        {
            enumerator = stack.Pop();
            enumerator.Dispose();
        }
    }
}

Eric Lippert's solution 不同,RecursiveSelect 直接与枚举器一起工作,因此它不需要调用 Reverse(它将整个序列缓冲在内存中)。

使用 RecursiveSelect,OP 的原始方法可以简单地改写如下:

public static IEnumerable<Control> GetDeepControlsByType<T>(this Control control)
{
    return control.Controls.RecursiveSelect(c => c.Controls).Where(c => c is T);
}

【讨论】:

  • 为了让这个(优秀的)代码工作,我不得不使用 'OfType 将 ControlCollection 转换为 IEnumerable 形式;在 Windows 窗体中,ControlCollection 不可枚举: return control.Controls.OfType().RecursiveSelect(c => c.Controls.OfType()) .Where(c => c is T );
【解决方案3】:

Seredynski's syntax 是正确的,但你应该小心避免在递归函数中使用yield return,因为这对内存使用来说是一场灾难。请参阅https://*.com/a/3970171/284795,它会随着深度爆炸性地扩展(类似的功能是在我的应用程序中使用 10% 的内存)。

一个简单的解决方案是使用一个列表并通过递归https://codereview.stackexchange.com/a/5651/754 传递它

/// <summary>
/// Append the descendents of tree to the given list.
/// </summary>
private void AppendDescendents(Tree tree, List<Tree> descendents)
{
    foreach (var child in tree.Children)
    {
        descendents.Add(child);
        AppendDescendents(child, descendents);
    }
}

或者,您可以使用堆栈和 while 循环来消除递归调用 https://codereview.stackexchange.com/a/5661/754

【讨论】:

    【解决方案4】:

    其他人为您提供了正确答案,但我认为您的案例不会从屈服中受益。

    这是一个sn-p,它实现了同样的效果而不屈服。

    public static IEnumerable<Control> GetDeepControlsByType<T>(this Control control)
    {
       return control.Controls
                     .Where(c => c is T)
                     .Concat(control.Controls
                                    .SelectMany(c =>c.GetDeepControlsByType<T>()));
    }
    

    【讨论】:

    • 不使用 LINQ yield吗? ;)
    • 这很漂亮。额外的foreach 循环一直困扰着我。现在我可以用纯函数式编程来做到这一点!
    • 我喜欢这个解决方案的可读性,但它面临与使用 yield 相同的迭代器性能问题。 @PhilippM:已验证 LINQ 使用 yield referencesource.microsoft.com/System.Core/R/…
    • 赞一个伟大的解决方案。
    【解决方案5】:

    我认为你必须 yield return 枚举中的每个控件。

        public static IEnumerable<Control> GetDeepControlsByType<T>(this Control control)
        {
            foreach (Control c in control.Controls)
            {
                if (c is T)
                {
                    yield return c;
                }
    
                if (c.Controls.Count > 0)
                {
                    foreach (Control childControl in c.GetDeepControlsByType<T>())
                    {
                        yield return childControl;
                    }
                }
            }
        }
    

    【讨论】:

      【解决方案6】:

      您需要在第二个 yield return

      中从枚举器返回 items,而不是枚举器本身
      public static IEnumerable<Control> GetDeepControlsByType<T>(this Control control)
      {
          foreach (Control c in control.Controls)
          {
              if (c is T)
              {
                  yield return c;
              }
      
              if (c.Controls.Count > 0)
              {
                  foreach (Control ctrl in c.GetDeepControlsByType<T>())
                  {
                      yield return ctrl;
                  }
              }
          }
      }
      

      【讨论】:

        【解决方案7】:

        你需要让递归调用产生的每一项

        public static IEnumerable<Control> GetDeepControlsByType<T>(this Control control)
        {
            foreach(Control c in control.Controls)
            {
                if (c is T)
                {
                    yield return c;
                }
        
                if(c.Controls.Count > 0)
                {
                    foreach (Control control in c.GetDeepControlsByType<T>())
                    {
                        yield return control;
                    }
                }
            }
        }
        

        请注意,以这种方式进行递归是有代价的 - 您最终会创建大量迭代器,如果您有一个非常深的控制树,这可能会产生性能问题。如果你想避免这种情况,你基本上需要自己在方法中进行递归,以确保只创建了一个迭代器(状态机)。有关更多详细信息和示例实现,请参阅 this question - 但这显然也增加了一定程度的复杂性。

        【讨论】:

        • 令我惊讶的是,在一个关于让步 Jon 的帖子中没有提到 c.Controls.Count &gt; 0.Any() :)
        • @Tymek 实际上在链接的答案中提到了。
        【解决方案8】:

        在返回 IEnumerable&lt;T&gt; 的方法中,yield return 必须返回 T,而不是 IEnumerable&lt;T&gt;

        替换

        yield return c.GetDeepControlsByType<T>();
        

        与:

        foreach (var x in c.GetDeepControlsByType<T>())
        {
          yield return x;
        }
        

        【讨论】: