Generic All Controls Method

前端 未结 1 818
一生所求
一生所求 2020-11-27 07:53

Couldn\'t think of a better title so appologies..

I\'m trying to convert this method, which will retrieve all child controls of a form, to be an extension method as

相关标签:
1条回答
  • 2020-11-27 08:51

    The problem is that Concat would want an IEnumerable<T> as well - not an IEnumerable<Control>. This should work though:

    public static IEnumerable<T> GetAll<T>(this Control control) where T : class
    {
        var controls = control.Controls.Cast<Control>();
    
        return controls.SelectMany(ctrl => GetAll<T>(ctrl))
                                    .Concat(controls.OfType<T>()));
    }
    
    0 讨论(0)
提交回复
热议问题