我在 foreach 循环中有一个嵌套的 while 循环,我想在满足某个条件时无限期地推进枚举数。为此,我尝试将枚举器强制转换为 IEnumerator< T > (如果它在 foreach 循环中则必须如此),然后在强制转换的对象上调用 MoveNext() 但它给了我一个错误,说我无法转换它。
无法通过引用转换、装箱转换、拆箱转换、包装转换或空类型转换将类型“System.DateTime”转换为 System.Collections.Generic.IEnumerator。
foreach (DateTime time in times)
{
while (condition)
{
// perform action
// move to next item
(time as IEnumerator<DateTime>).MoveNext(); // will not let me do this
}
// code to execute after while condition is met
}
在 foreach 循环中手动增加 IEnumerator 的最佳方法是什么?
编辑:编辑显示while循环之后有代码,一旦满足条件,我想执行这就是为什么我想在while内手动递增然后打破它而不是继续,这会让我回到顶部. 如果这不可能,我相信最好的办法是重新设计我的工作方式。