2
有關於我寫了,看起來像這樣的擴展方法的問題:的ControlCollection擴展方法優化
public static IEnumerable<T> FindControlsOfType<T>(this ControlCollection instance) where T : class
{
T control;
foreach (Control ctrl in instance)
{
if ((control = ctrl as T) != null)
{
yield return control;
}
foreach (T child in FindControlsOfType<T>(ctrl.Controls))
{
yield return child;
}
}
}
public static IEnumerable<T> FindControlsOfType<T>(this ControlCollection instance, Func<T, bool> match) where T : class
{
return FindControlsOfType<T>(instance).Where(match);
}
這裏的想法是要找到匹配specifc標準(因此Func鍵<所有的控件..> )在控件集合中。我的問題是:
第二種方法(具有Func)首先調用第一種方法來查找類型T的所有控件,然後執行where條件或執行「運行時」優化調用以執行where條件在「整體」列舉(如果你明白我的意思)。
其次,有沒有其他優化方法可以使代碼更好地執行。
一個例子是這樣的:
var checkbox = this.Controls.FindControlsOfType<MyCustomCheckBox>(
ctrl => ctrl.CustomProperty == "Test"
)
.FirstOrDefault();
對於性能問題,也許看看:http://msmvps.com/blogs/jon_skeet/archive/2010/03/25/just-how-lazy-are-you.aspx – Oliver 2010-04-23 08:25:40
正是我所在尋找 - 爲我節省了一些時間,謝謝 – 2011-12-21 17:49:18