得到迭代器 [英] get the iterator
问题描述
我需要从任何通用集合中获取迭代器。
公共类.... GetIterator(对象集合)
{
.....
}
如何用反射做到这一点?
提前致谢。
I need to get a iterator from any generic collection.
public class .... GetIterator(Object collection)
{
.....
}
How can I do this with reflection?
Thanks in advance.
推荐答案
" Marco" < TE ****** @ yahoo.com>在消息中写道
news:11 ********************** @ g44g2000cwa.googlegr oups.com ...
"Marco" <te******@yahoo.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
我需要从任何泛型集合中获取迭代器。
公共类.... GetIterator(对象集合)
...
}
我怎么能用反射做这个?
I need to get a iterator from any generic collection.
public class .... GetIterator(Object collection)
{
....
}
How can I do this with reflection?
我不确定我跟着你。集合(意味着任何实现
ICollection的类)隐式实现IEnumerable。这意味着您可以在它们上面调用
GetEnumerator来获取枚举器(迭代器)。没有必要反映
。
如果我误解了,请随时回复。
-
问候,
Tim Haughton
Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
Marco写道:
Marco wrote:
我需要从任何泛型集合中获取迭代器。
public class .... GetIterator(Object collection)
> ....
}
如何通过反射来做到这一点?
I need to get a iterator from any generic collection.
public class .... GetIterator(Object collection)
{
....
}
How can I do this with reflection?
你为什么要这样做?这有反思吗?如果你知道给定的类型是
a集合并且它有一个迭代器,你可以将它转换为
IEnumerable并从那里获取迭代器:
IEnumerable enumerable =集合为IEnumerable;
if(enumerable!= null){
//从enumerable.GetEnumerator()获取迭代器
//或者直接和foreach一起使用
}
Oliver Sturm
-
提供专家编程和咨询服务
请参阅 http ://www.sturmnet.org (尝试/博客)
Why would you do this with reflection? If you know that the given type is
a collection and it has an iterator, you can just cast it to an
IEnumerable and get the iterator from there:
IEnumerable enumerable = collection as IEnumerable;
if (enumerable != null) {
// get the iterator from enumerable.GetEnumerator()
// or just use it directly with foreach
}
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
" Oliver Sturm" <醇**** @ sturmnet.org>在消息中写道
news:xn **************** @ msnews.microsoft.com ...
"Oliver Sturm" <ol****@sturmnet.org> wrote in message
news:xn****************@msnews.microsoft.com...
Oliver Sturm
-
提供专家编程和咨询服务
请参阅 http:// www.sturmnet.org (尝试/博客)
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
嗨奥利弗,我看到你的名字'与休麦克劳德和chums在12月的伦敦极客晚宴上获得了
。我希望能够自己制作 - 这将是很好的
来为Usenet帖子添加一张脸。
-
此致,
Tim Haughton
Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
这篇关于得到迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!