473,756 Members | 6,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

get the iterator

I need to get a iterator from any generic collection.
public class .... GetIterator(Obj ect collection)
{

.....

}

How can I do this with reflection?

Thanks in advance.

Nov 17 '05 #1
18 2172
"Marco" <te******@yahoo .com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
I need to get a iterator from any generic collection.
public class .... GetIterator(Obj ect collection)
{

....

}

How can I do this with reflection?


I'm not sure I'm following you. Collections (meaning any class implementing
ICollection) implicitly implement IEnumerable. This means that you can call
GetEnumerator on them to get an enumerator (iterator). There's no need for
reflection.

If I've misunderstood feel free to post back.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
Nov 17 '05 #2
Marco wrote:
I need to get a iterator from any generic collection.
public class .... GetIterator(Obj ect collection)
{

....

}

How can I do this with reflection?


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.GetE numerator()
// or just use it directly with foreach
}

Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Nov 17 '05 #3
"Oliver Sturm" <ol****@sturmne t.org> wrote in message
news:xn******** ********@msnews .microsoft.com. ..
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)


Hi Oliver, I see your name's down for the nosh with Hugh MacLeod and chums
at December's London Geek dinner. I hope to make it myself - it will be nice
to put a face to the Usenet posts.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
Nov 17 '05 #4
Thanks very much.I would like control tha collection implement
IEnumerable with reflection.

Can I put your code in a function and and return a IEnumerable or null?
How?

Nov 17 '05 #5
Hi Tim,

Tim Haughton wrote:
Hi Oliver, I see your name's down for the nosh with Hugh MacLeod and chums
at December's London Geek dinner. I hope to make it myself - it will be
nice
to put a face to the Usenet posts.


Yes, that would be great. I haven't met most of the guys (and girls!) yet
myself, so I'm looking forward to that. Hope you can make it!
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Nov 17 '05 #6
Marco wrote:
Thanks very much.I would like control tha collection implement
IEnumerable with reflection.
I'm sorry, I don't understand exactly what you want to do with Reflection.
Can I put your code in a function and and return a IEnumerable or null?
How?


The magic of the "as" operator makes that extremely easy:

IEnumerable GetIEnumerable( object potentialCollec tion) {
return potentialCollec tion as IEnumerable;
}
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Nov 17 '05 #7
it's right.
Thanks very much.
I would like control that collection implement IEnumerable with
reflection but it's a wrong way.

Nov 17 '05 #8


Oliver Sturm wrote:
IEnumerable enumerable = collection as IEnumerable;
if (enumerable != null) {
// get the iterator from enumerable.GetE numerator()
// or just use it directly with foreach
}


Why use "collection as IEnumerable"? If it's supposed to be a collection
just use a cast. What would be a reasonable behaviour if collection is
not IEnumerable?

I'd write:

IEnumerator it = ((IEnumerable)c ollection).GetE numerator();

or for iteration:

foreach ( Foo foo in ((IEnumerable)c ollection )
...

--
Helge Jensen
mailto:he****** ****@slog.dk
sip:he********* *@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #9
Helge Jensen wrote:
Why use "collection as IEnumerable"? If it's supposed to be a collection
just use a cast. What would be a reasonable behaviour if collection is not
IEnumerable?

I'd write:

IEnumerator it = ((IEnumerable)c ollection).GetE numerator();

or for iteration:

foreach ( Foo foo in ((IEnumerable)c ollection )
...

Well, the way the sample code is given (and requested by the OP) there
wasn't really any guarantee that the object would be a collection. In that
case the cast might be unsafe, that's why I'm using "as". The whole method
the OP was posting about would be kind of pointless if it had a parameter
of a collection type to start with, wouldn't it? :-)

And it's better to use

IEnumerable enumerable = collection as IEnumerable;
if (enumerable != null)
...

than

if (collection is IEnumerable) {
IEnumerable enumerable = (IEnumerable) collection;
...
}

because with the "as" operator, you get to check for the correct type and
acquire the interface at the same time. I usually use casts only if either
I'm completely sure that the target type is available or if, for some
reason, I want to assert that the target type *must* be available at the
point.
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
3684
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages mentioned: I characterized one way of looking at languages in this way: a lot of them are either the agglutination of features or they're a crystallization of style. Languages such as APL, Lisp, and Smalltalk are what you might call style...
26
1514
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class Iterator { public : typedef typename Set::value_type T; typedef typename Set::iterator SetIterator; Iterator(Set& container, const SetIterator& it);
0
1945
by: nick | last post by:
Hi, I need to manage a "layered" collection of objects, where each layer grows independently, e.g, o--+--+--+--+--+ 1st layer | o 2nd layer (empty) | o--+--+--+ 3rd layer |
14
4876
by: shawnk | last post by:
I searched the net to see if other developers have been looking for a writable iterator in C#. I found much discussion and thus this post. Currently (C# 2) you can not pass ref and out arguments to an iterator method (one returning IEnumerable). I WOULD like to do this for transformative operations on a collection. I realize the need to lock target, etc. Does anyone know how to handle 'writable iterators' in C# 2?
0
2681
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never looked for one on the net (I did browse the boost library though). It doesn't matter right now, anyway. To put it simply, Abstract Iterator is mainly a wrapper class. It helps
21
5718
by: T.A. | last post by:
I understand why it is not safe to inherit from STL containers, but I have found (in SGI STL documentation) that for example bidirectional_iterator class can be used to create your own iterator classes by inheriting from it, ie. class my_bidirectional_iterator : public bidirectional_iterator<double> { ... };
16
2582
by: mailforpr | last post by:
How do I do that? The thing is, the only information I have about the iterator is the iterator itself. No container it is belonging to or anything. Like template<Iteratorvoid totally_isolated(Iterator& it) { //how do I find out if it points to the end node? }
27
5315
by: Steven D'Aprano | last post by:
I thought that an iterator was any object that follows the iterator protocol, that is, it has a next() method and an __iter__() method. But I'm having problems writing a class that acts as an iterator. I have: class Parrot(object): def __iter__(self): return self def __init__(self): self.next = self._next()
4
2007
by: mkborregaard | last post by:
Hi, I have the weirdest problem, and I can not see what is going wrong. I have made a 2d container class, and am implementing an iterator for that class. However, the ++ operator is behaving very strange. The implementation looks like this (there is a lot of code, but I have only included it all for consistency. The problem is quite small and local): template<typename T> class Row<T> : public std::vector<T> {}; template<typename...
2
2289
by: Terry Reedy | last post by:
Luis Zarrabeitia wrote: Interesting observation. Iterators are intended for 'iterate through once and discard' usages. To zip a long sequence with several short sequences, either use itertools.chain(short sequences) or put the short sequences as the first zip arg. To test without consuming, wrap the iterator in a trivial-to-write one_ahead or peek class such as has been posted before.
0
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10040
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9873
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.