我们的项目目前使用 2 种方法在页面内查找控件。第一种是递归使用 .FindControl。 另一种是像这样使用LINQ:
(from n in Page.Controls.Cast<Control>().Descendants(c => c.Controls.Cast<Control>())
where (n as Label != null && n.ID == "TaskIDLabel")
select n).First() as Label;
哪个使用此扩展:
static public IEnumerable<T> Descendants<T>(this IEnumerable<T> source,
Func<T, IEnumerable<T>> DescendBy)
{
foreach (T value in source)
{
yield return value;
foreach (T child in DescendBy(value).Descendants<T>(DescendBy))
{
yield return child;
}
}
}
这两种方法哪个更好?哪个更快?