【发布时间】:2014-12-02 12:33:12
【问题描述】:
我刚刚看到下面的代码:
public void ListControls(ControlCollection controls, List<Control> controlsFound)
{
foreach (var control in controls)
{
if (control is IAttributeAccessor)
{
controlsFound.Add(control); //Error (Invalid argument to Add method)
ListControls(control.Controls, controlsFound);
}
}
}
上面报错:
如果我将foreach 中的var 更改为Control,那么它可以工作。原因是 Add 方法期望 Control 作为参数。但我认为 var 应该已经被 Control 隐式替换了,对吧?
【问题讨论】:
-
ControlCollection没有实现IEnumerable<Control>
标签: c# asp.net var anonymous-types