【发布时间】:2013-07-03 17:21:08
【问题描述】:
想不出更好的标题,所以很抱歉..
我正在尝试将this method(它将检索表单的所有子控件)转换为扩展方法并接受接口作为输入。到目前为止,我最多
public IEnumerable<Control> GetAll<T>(this Control control) where T : class
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetAll<T>(ctrl))
.Concat(controls)
.Where(c => c is T);
}
除了我需要在调用它以访问其属性时添加OfType<T>() 之外,它工作正常。
例如(这个 == 形式)
this.GetAll<IMyInterface>().OfType<IMyInterface>()
我正在努力将返回类型变成通用返回类型IEnumerable<T>,这样我就不必包含OfType,它只会返回相同的结果但可以正确转换。
大家有什么建议吗?
(将返回类型更改为IEnumerable<T> 会导致Concat 抛出
实例参数:无法从 'System.Collections.Generic.IEnumerable
<T>' 转换为 'System.Linq.ParallelQuery<System.Windows.Forms.Control>'
【问题讨论】:
-
用“public IEnumerable
”替换“public IEnumerable ”? -
导致底部的错误被抛出