我有以下函数来获取所有特定类型的子控件。
我正在尝试使用通用类型参数进行转换。
代码在
private IEnumerable<Control> GetControlsOfType(Control control, Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetControlsOfType(ctrl, type))
.Concat(controls)
.Where(c => c.GetType() == type);
}
我正在尝试使用通用类型参数进行转换。
private IEnumerable<T> GetControlsOfType<T>(Control control) where T: Control
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetControlsOfType<T>(ctrl))
.Concat(controls)
.Where(c => c.GetType() == T);
}
代码在
.Concat
处出现错误。
如何解决这个问题?错误6:'System.Collections.Generic.IEnumerable
<T
>'不包含定义为“Concat”的方法,而最佳的扩展方法重载'System.Linq.Queryable.Concat<TSource
>(System.Linq.IQueryable<TSource
>, System.Collections.Generic.IEnumerable<TSource
>)'存在一些无效参数。
IEnumerable<T>
-- 这样不会返回IEnumerable<Control>
吗? - Jon BGetControlsOfType<T>(ctrl)
已经具有IEnumerable<Control>
类型。为什么这里需要OfType<Control>()
? - ca9163d9'System.Collections.Generic.IEnumerable<T>'
没有包含 'Concat' 的定义,但是IEnumerable<Control>
包含。你经常会发现相同的问题与具有相同解决方法的Collection
:) - dav_iIEnumerable<T>
创建Concat
。 - ca9163d9