C# 将类型'System.Collections.IEnumerator'转换为'System.Collections.Generic.IEnumerator'`

C# 将类型'System.Collections.IEnumerator'转换为'System.Collections.Generic.IEnumerator'`,c#,.net,C#,.net,我有以下代码: // row is a DatGridViewRow object IEnumerator<DataGridCell> cells = row.Cells.GetEnumerator(); //行是DatGridViewRow对象 IEnumerator cells=row.cells.GetEnumerator(); 我得到编译错误,指定我 无法将System.Collections.IEnumerator类型隐式转换为 System.Collections.G

我有以下代码:

// row is a DatGridViewRow object
IEnumerator<DataGridCell> cells = row.Cells.GetEnumerator();
//行是DatGridViewRow对象
IEnumerator cells=row.cells.GetEnumerator();
我得到编译错误,指定我

无法将System.Collections.IEnumerator类型隐式转换为 System.Collections.Generic.IEnumerator

我需要做一个明确的演员阵容。当我试着通过

IEnumerator<DataGridCell> cells = (IEnumerator<DataGridCell>)row.Cells.GetEnumerator();
IEnumerator cells=(IEnumerator)row.cells.GetEnumerator();
我得到一个运行时错误


有什么想法吗?

行.Cells.GetEnumerator()
返回一个
IEnumerator
,但您试图将其分配给一个
IEnumerator
,该操作无法完成,因此会出现异常。在它前面添加
(IEnumerator)
不会有帮助,因为它仍然是另一种类型

要实现此目的,请在以下操作之前使用
.Cast

IEnumerator<DataGridCell> cells = row.Cells.Cast<DataGridViewCell>().GetEnumerator();
IEnumerator cells=row.cells.Cast().GetEnumerator();
在我看来,更好的选择是使用
IEnumerable

IEnumerable cells=row.cells.Cast();

行.Cells.GetEnumerator()
返回一个
IEnumerator
,但您试图将其分配给一个
IEnumerator
,该操作无法完成,因此会出现异常。在它前面添加
(IEnumerator)
不会有帮助,因为它仍然是另一种类型

要实现此目的,请在以下操作之前使用
.Cast

IEnumerator<DataGridCell> cells = row.Cells.Cast<DataGridViewCell>().GetEnumerator();
IEnumerator cells=row.cells.Cast().GetEnumerator();
在我看来,更好的选择是使用
IEnumerable

IEnumerable cells=row.cells.Cast();

首先尝试使用cast操作符

IEnumerator<DataGridCell> cells = row.Cells.Cast<DataGridCell>().GetEnumerator();
IEnumerator cells=row.cells.Cast().GetEnumerator();

首先尝试使用cast操作符

IEnumerator<DataGridCell> cells = row.Cells.Cast<DataGridCell>().GetEnumerator();
IEnumerator cells=row.cells.Cast().GetEnumerator();
IEnumerator cells=row.cells.Cast().GetEnumerator();
对于在家中跟随的人:

var able = (IEnumerable)new List<String>();

IEnumerator<String> tor = able.Cast<String>().GetEnumerator();
var-able=(IEnumerable)新列表();
IEnumerator or=able.Cast().GetEnumerator();
我不明白为什么OP想要IEnumerator而不是IEnumerable(事实上,我怀疑他可能更喜欢后者),但这是他问的问题

IEnumerator cells=row.cells.Cast().GetEnumerator();
对于在家中跟随的人:

var able = (IEnumerable)new List<String>();

IEnumerator<String> tor = able.Cast<String>().GetEnumerator();
var-able=(IEnumerable)新列表();
IEnumerator or=able.Cast().GetEnumerator();

我不明白为什么OP想要IEnumerator而不是IEnumerable(事实上,我怀疑他可能更喜欢后者),但这是他问的问题

试试
.GetEnum().CastTo()
也许这会有帮助:试试
.GetEnum().CastTo()
也许这会有帮助:Downvoter请解释我没有投你的反对票,但OP说他想要的是
IEnumerator
,而不是
IEnumerable
。问他是否确定这真的对他合适是公平的,但你不能只说“还有别的事,祝你好运”。@EdPlunkett-Ya:)刚刚看到我的错误并改正了:)@Lawrence Vo-这有助于你解决问题吗?@GiladGreen是的,谢谢!请解释我没有投你反对票,但OP说他想要的是
IEnumerator
,而不是
IEnumerable
。问他是否确定这真的对他合适是公平的,但你不能只说“还有别的事,祝你好运”。@EdPlunkett-Ya:)刚刚看到我的错误并改正了:)@Lawrence Vo-这有助于你解决问题吗?@GiladGreen是的,谢谢!谢谢你纠正我:)@GiladGreen我的荣幸!谢谢你纠正我:)@GiladGreen我的荣幸!