ChatGPT解决这个技术问题 Extra ChatGPT

IEnumerable 和 Recursion 使用 yield return

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

该方法是递归的,当 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<Object>,则代码会构建,但输出中会返回错误的类型。

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

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

M
Marcin Seredynski

在返回 IEnumerable<T> 的方法中,yield return 必须返回 T,而不是 IEnumerable<T>

代替

yield return c.GetDeepControlsByType<T>();

和:

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

C
Community

您需要产生递归调用产生的每个项目:

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 > 0.Any() :)
@Tymek 实际上在链接的答案中提到了它。
C
Community

正如 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 );
J
Jamie Dixon

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

这是一个片段,它可以在不产生任何效果的情况下实现相同的效果。

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/…
大拇指为一个很好的解决方案。
R
Rob Levine

您需要在第二个 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;
            }
        }
    }
}

T
Torbjörn Hansson

我认为您必须返回可枚举中的每个控件。

    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;
                }
            }
        }
    }

C
Community

Seredynski's syntax 是正确的,但您应该小心避免在递归函数中使用 yield return,因为它对内存使用来说是一场灾难。请参阅 https://stackoverflow.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


y
yoel halb

虽然那里有很多好的答案,但我仍然要补充一点,可以使用 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 是不必要地验证已经保证唯一的序列的唯一性,因此是客观的降级。