【发布时间】:2010-11-26 08:42:59
【问题描述】:
我正在尝试将 Dropdown 控件的集合作为参数传递给将 Control 类型的集合作为输入的方法。执行时出现以下错误:
“无法转换类型为 'd__a31[System.Web.UI.WebControls.DropDownList]' to type 'System.Collections.Generic.IEnumerable1[System.Web.UI.Control]' 的对象。”
知道我为什么会得到这个吗?
我的代码:
private void Caller()
{
IEnumerable<DropDownList> dropDownControlsInCurrentRow = currentRow.Controls.OfType<DropDownList>();
SetControlsVisibility(dropDownControlsInCurrentRow, false);
}
private void SetControlsVisibility(IEnumerable<Control> controlCollection, bool visibilityFlag)
{
foreach (ctrl in controlCollection) {
ctrl.Visible = visibilityFlag;
}
}
【问题讨论】:
-
OfType方法是你的还是框架的?如果是你的,你能把代码放上来吗?如果来自框架,currentRow的类型是什么? -
^^ 它是 IEnumerable 扩展方法(内置)。 currentRow 是 GridViewRow 类型的。
标签: c# asp.net linq generics extension-methods