将 IEnumerator 转换为通用 IEnumerator 的最佳方法是什么?
我正在 C#.NET 3.5 中为自定义 ConfigurationHandler 编写自定义 ConfigurationElementCollection,并且希望将 IEnumerator 公开为通用 IEnumerator。
实现这一目标的最佳方法是什么?
我目前正在使用代码:
public new IEnumerator<GenericObject> GetEnumerator() { var list = new List(); var baseEnum = base.GetEnumerator(); while(baseEnum.MoveNext()) { var obj = baseEnum.Current as GenericObject; if (obj != null) list.Add(obj); } return list.GetEnumerator(); }
干杯
I am writing a custom ConfigurationElementCollection for a custom ConfigurationHandler in C#.NET 3.5 and I am wanting to expose the IEnumerator as a generic IEnumerator.
What would be the best way to achieve this?
I am currently using the code:
public new IEnumerator<GenericObject> GetEnumerator() { var list = new List(); var baseEnum = base.GetEnumerator(); while(baseEnum.MoveNext()) { var obj = baseEnum.Current as GenericObject; if (obj != null) list.Add(obj); } return list.GetEnumerator(); }
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不相信框架中有任何内容,但您可以轻松编写一个:
从 LINQ 调用
Enumerable.Cast
然后调用GetEnumerator()
结果 - 但如果您的类已经实现了IEnumerable
并且T
是值类型,则充当无操作,因此GetEnumerator ()
调用会递归并抛出StackOverflowException
。 当foo
绝对是一个不同对象(不委托)时,使用return foo.Cast.GetEnumerator();
是安全的回到这个)但除此之外,您可能最好使用上面的代码。I don't believe there's anything in the framework, but you could easily write one:
It's tempting to just call
Enumerable.Cast<T>
from LINQ and then callGetEnumerator()
on the result - but if your class already implementsIEnumerable<T>
andT
is a value type, that acts as a no-op, so theGetEnumerator()
call recurses and throws aStackOverflowException
. It's safe to usereturn foo.Cast<T>.GetEnumerator();
whenfoo
is definitely a different object (which doesn't delegate back to this one) but otherwise, you're probably best off using the code above.IEnumerable
已派生自IEnumerable
,因此无需进行任何转换。 您可以简单地转换为它......实际上它是隐式的,不需要转换。对于 LINQ,反之亦然并不困难:
IEnumerable<T>
already derives fromIEnumerable
so there's no need to do any conversion. You can simply cast to it...well actually it's implicit no cast necessary.Going the other way round isn't much more difficult with LINQ:
您可以使用
OfType
和Cast
。要获取
IEnumerator
而不是IEnumerable
,您只需调用GetEnumerator()
You can use
OfType<T>
andCast<T>
.To get an
IEnumerator<T>
instead of anIEnumerable<T>
you can just make a call toGetEnumerator()
我遇到了一些评论中提到的同样的堆栈溢出问题。 就我而言,这是因为 GetEnumerator 调用需要对 base.GetEnumerator 进行调用,否则您将在自己的 GetEnumerator 重新定义中循环。
这是抛出堆栈溢出的代码。 使用 foreach 语句调用我试图重载的相同 GetEnumerator 函数:
我最终得到了原始帖子的简化版本,因为您不需要使用列表持有者。
I was running into the same Stack Overflow problem mentioned is some of the comments. In my case it was due to the fact that the GetEnumerator call needed to be to the base.GetEnumerator otherwise you loop within your own GetEnumerator redefinition.
This is the code that was Throwing the Stack Overflow. The use of the foreach statement call the same GetEnumerator function I'm trying to overload:
I've ended up with a simplified version of the original post as you don't need to use a List holder.
这对我有用。
This works for me.