Home All Groups Group Topic Archive Search About
Author
10 Jan 2007 4:41 PM
Kevin Burton
I see that there is no equivalent for generation a "foreach" statement using
CodeDom. I was wondering what would be a reasonable substitution or
work-around?

Thank you.

Kevin

Author
10 Jan 2007 5:26 PM
Mubashir Khan
Happen to find this at a forum.


Hi guys,

for some good reason I dont remember the CodeDom cannot generate for each
statement like the one below:

foreach (DataRow row in dt.Rows)
{
    myList.Add(ConvertToObject(row));
}



What is the alternative and How Can I generate it?

thanks a lot in advance

---------------------------------------------

I do not think that there is a way to generate a foreach statement from
CodeDom. My guess for this is because foreach isn't a statement in IL - it
is really syntactically identical to the following:

IEnumerator e = dt.Rows.GetEnumerator();
while(e.MoveNext())
{
    DataRow row = (DataRow)e.Current;
    myList.Add(ConvertToObject(row));
}

You can generate a while loop using the CodeIterationStatement class, and
just not specify the InitStatement and IncrementStatement properties.


Show quote
"Kevin Burton" <KevinBur***@discussions.microsoft.com> wrote in message
news:99D6F927-C878-44FD-A8F3-C5FD86EA1F52@microsoft.com...
>I see that there is no equivalent for generation a "foreach" statement
>using
> CodeDom. I was wondering what would be a reasonable substitution or
> work-around?
>
> Thank you.
>
> Kevin
Author
10 Jan 2007 7:59 PM
Kevin Burton
Sorry, one more question. Given a Type how do I tell if IEnumerable or
IEnumerate is supported? Then from the Type how do I get the Type that
IEnumerate would return? In other words what is the Type of the Current
method?

Thank you.

Show quote
"Mubashir Khan" wrote:

> Happen to find this at a forum.
>
>
> Hi guys,
>
> for some good reason I dont remember the CodeDom cannot generate for each
> statement like the one below:
>
>  foreach (DataRow row in dt.Rows)
>  {
>     myList.Add(ConvertToObject(row));
>  }
>
>
>
> What is the alternative and How Can I generate it?
>
> thanks a lot in advance
>
> ---------------------------------------------
>
> I do not think that there is a way to generate a foreach statement from
> CodeDom. My guess for this is because foreach isn't a statement in IL - it
> is really syntactically identical to the following:
>
> IEnumerator e = dt.Rows.GetEnumerator();
> while(e.MoveNext())
> {
>     DataRow row = (DataRow)e.Current;
>     myList.Add(ConvertToObject(row));
> }
>
> You can generate a while loop using the CodeIterationStatement class, and
> just not specify the InitStatement and IncrementStatement properties.
>
>
> "Kevin Burton" <KevinBur***@discussions.microsoft.com> wrote in message
> news:99D6F927-C878-44FD-A8F3-C5FD86EA1F52@microsoft.com...
> >I see that there is no equivalent for generation a "foreach" statement
> >using
> > CodeDom. I was wondering what would be a reasonable substitution or
> > work-around?
> >
> > Thank you.
> >
> > Kevin
>
>
>
Author
11 Jan 2007 7:24 PM
Ben Voigt
"Kevin Burton" <KevinBur***@discussions.microsoft.com> wrote in message
news:F3B3C9DE-D7DA-4819-9365-760375492ECD@microsoft.com...
> Sorry, one more question. Given a Type how do I tell if IEnumerable or
> IEnumerate is supported? Then from the Type how do I get the Type that
> IEnumerate would return? In other words what is the Type of the Current
> method?

Type.GetInterfaces()

then check each against System.Collections.IEnumerable,
System.Collections.IEnumerator, System.Collections.Generic.IEnumerable<T>,
and System.Collections.Generic.IEnumerator<T>.

If either of the generic interfaces are implemented, you can get the type
returned during enumeration using Type.GetGenericArguments() on the
interface in question.  If only one of the System.Collections interfaces is
listed, the enumerator gives objects.

Show quote
>
> Thank you.
>
> "Mubashir Khan" wrote:
>

AddThis Social Bookmark Button