使用yield return的IEnumerable和Recursion

Jam*_*xon 298 c# generics ienumerable yield

我有一个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>();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

这当前抛出"无法转换表达式类型"错误.但是IEnumerable<Object>,如果此方法返回类型,则代码构建,但输出中返回错误的类型.

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

Mar*_*ski 480

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

更换

yield return c.GetDeepControlsByType<T>();
Run Code Online (Sandbox Code Playgroud)

有:

foreach (var x in c.GetDeepControlsByType<T>())
{
  yield return x;
}
Run Code Online (Sandbox Code Playgroud)


Jon*_*eet 97

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

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;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

  • 我发现令人惊讶的是,在一个关于屈服于Jon的线程中没有提到`c.Controls.Count> 0`与`.Any()`:) (2认同)

Mic*_*Liu 25

正如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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Eric Lippert的解决方案不同,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);
}
Run Code Online (Sandbox Code Playgroud)

  • 为了让这段(优秀的)代码正常工作,我必须使用 'OfType 将 ControlCollection 转换为 IEnumerable 形式;在 Windows 窗体中,ControlCollection 不可枚举: return control.Controls.OfType&lt;Control&gt;().RecursiveSelect&lt;Control&gt;(c =&gt; c.Controls.OfType&lt;Control&gt;()) .Where(c =&gt; c is T ); (2认同)

tym*_*tam 16

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

这是一个实现同样而不屈服的片段.

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>()));
}
Run Code Online (Sandbox Code Playgroud)

  • 不使用LINQ`products`吗?;) (2认同)

Rob*_*ine 12

您需要在第二个中返回枚举器中的项目,而不是枚举器本身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 ctrl in c.GetDeepControlsByType<T>())
            {
                yield return ctrl;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Tor*_*son 9

我认为你必须返回枚举中的每个控件.

    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;
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)


Col*_*nic 7

Seredynski的语法是正确的,但你应该小心避免yield return递归函数,因为它是内存使用的灾难.请参阅/sf/answers/277912001/,它会随着深度进行爆炸性扩展(类似的功能在我的应用中使用了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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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