var关键字不推断RepeaterItem的类型,为什么呢?

Gal*_*you 1 c# compiler-construction

这是一个快速的.我有以下代码:

foreach (var item in myRepeater.Items)
{
    MyViewModelItem x = new MyViewModelItem();
    MapToEntity(x, item);
    myList.Add(report);
}

void MapToEntity(object entity, Control control);
Run Code Online (Sandbox Code Playgroud)

我希望这个代码编译没有问题.然而,它没有.
它导致编译时错误,说"MapToEntity"方法有一些无效的参数.编译器无法推断RepeaterItem的类型,它将其识别为普通的System.Object.

为什么会这样?我错过了什么吗?
Ps:我通过删除var关键字并明确定义项目"RepeaterItem"的类型来修复代码.

Ant*_*lev 6

RepeaterItemCollection不实现IEnumerable<RepeaterItem>普通的IEnumerable.因此,编译器不可能推断出类型.

  • walkaround将是`Items.OfType <RepeaterItems>()`或`Items.Cast <RepeaterItems>()` (2认同)