473,320 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Nested Iterators & C# 3.0 yeilds problems?

Hi All,
I have the following little class of iterators that allow me to iterate over
elements in the file system. I have nested some of them, and then added Func<FileInfo,
booldelegates to filter out unwanted files. I get an InvalidProgramException
and haven't (yet) been able to find out what's going on. Do you have any
ideas?

TIA

Andrew Matthews
========================================

public class FsIter
{
public static IEnumerable<DirectoryInfoSubDirectories(DirectoryI nfo
di)
{
yield return di;
foreach (DirectoryInfo directory in di.GetDirectories())
{
foreach (DirectoryInfo subDirectory in SubDirectories(directory))
{
yield return subDirectory;
}
}
}

public static IEnumerable<FileInfoAllFilesUnder(DirectoryInfo d)
{
foreach (DirectoryInfo dir in SubDirectories(d))
{
foreach (FileInfo fileInfo in dir.GetFiles())
{
yield return fileInfo;
}
}
}

public static IEnumerable<FileInfoMatchingFilesUnder(Func<FileIn fo,
boolx, DirectoryInfo d)
{
foreach (FileInfo fileInfo in AllFilesUnder(d))
{
if (x(fileInfo))
yield return fileInfo;
}
}

public static IEnumerable<FileInfoFilesByExtension(string ext,
DirectoryInfo d)
{
Func<FileInfo, booltest = x=>x.Extension==ext;
return Match(test, AllFilesUnder(d));
}

public static IEnumerable<ITMatch<IT>(Func<IT, booltest, IEnumerable<IT>
col)
{
foreach (IT it in col)
{
if(test(it))
yield return (it);
}
}
}

I tried to exercise it with the following:

[Test]public void TestIterateTestDir2()
{
string testdir = @"C:\etc\dev\fstags\src\UnitTests\tests";
List<FileInfofiles = new List<FileInfo>(FsTags.Core.FsIter.FilesByExtension (".txt",
new DirectoryInfo(testdir)));
Assert.AreEqual(files.Count, 4);
}

And I got the following:

System.InvalidProgramException: Common Language Runtime detected an invalid
program.
at FsTags.Core.FsIter.FilesByExtension(String ext, DirectoryInfo d)
at fstags.UnitTests.TestFsIter.TestIterateTestDir2() in TestFsIter.cs:line
26

I am running C# 3.0 May CTP (LINQ preview) and ReSharper 2.0 (2005)
Jul 16 '06 #1
6 1574
Andrew Matthews <ma*************@gmail.comwrote:
Hi All,
You have posted to 5 different newsgroups. Don't you think that
..languages.csharp would have sufficed?
I get an InvalidProgramException
I have adapted your source code to be a complete, compiling sample. It
does not generate an InvalidProgramException. Can you create a complete,
compiling sample which produces the error?

---8<---
using System;
using System.IO;
using System.Query;
using System.Collections.Generic;

public class App
{
public static IEnumerable<DirectoryInfo>
SubDirectories(DirectoryInfo di)
{
yield return di;
foreach (DirectoryInfo directory in di.GetDirectories())
foreach (DirectoryInfo subDirectory in
SubDirectories(directory))
yield return subDirectory;
}

public static IEnumerable<FileInfoAllFilesUnder(DirectoryInfo d)
{
foreach (DirectoryInfo dir in SubDirectories(d))
foreach (FileInfo fileInfo in dir.GetFiles())
yield return fileInfo;
}

public static IEnumerable<FileInfo>
MatchingFilesUnder(Func<FileInfo,boolx, DirectoryInfo d)
{
foreach (FileInfo fileInfo in AllFilesUnder(d))
if (x(fileInfo))
yield return fileInfo;
}

public static IEnumerable<FileInfoFilesByExtension(string ext,
DirectoryInfo d)
{
Func<FileInfo, booltest = x=>x.Extension==ext;
return Match(test, AllFilesUnder(d));
}

public static IEnumerable<ITMatch<IT>(Func<IT, booltest,
IEnumerable<ITcol)
{
foreach (IT it in col)
if(test(it))
yield return (it);
}

static void Main()
{
string testdir = @"C:\Program Files";
List<FileInfofiles =
new List<FileInfo>(FilesByExtension(".txt",
new DirectoryInfo(testdir)));
Console.WriteLine(files.Count);
}
}
--->8---
I am running C# 3.0 May CTP (LINQ preview) and ReSharper 2.0 (2005)
I am running C# 8.00.50916 on 2.0.50727.

-- Barry

--
http://barrkel.blogspot.com/
Jul 16 '06 #2
Hello Barry,

Considering that I wasn't sure whether the problem was with C# specifically
or with the CLR I thought I wouldn't take any chances of getting a response.
As for compiling code that gives the error - that was what I posted.

Andrew
Andrew Matthews <ma*************@gmail.comwrote:
>Hi All,
You have posted to 5 different newsgroups. Don't you think that
.languages.csharp would have sufficed?
>I get an InvalidProgramException
I have adapted your source code to be a complete, compiling sample. It
does not generate an InvalidProgramException. Can you create a
complete, compiling sample which produces the error?

---8<---
using System;
using System.IO;
using System.Query;
using System.Collections.Generic;
public class App
{
public static IEnumerable<DirectoryInfo>
SubDirectories(DirectoryInfo di)
{
yield return di;
foreach (DirectoryInfo directory in di.GetDirectories())
foreach (DirectoryInfo subDirectory in
SubDirectories(directory))
yield return subDirectory;
}
public static IEnumerable<FileInfoAllFilesUnder(DirectoryInfo d)
{
foreach (DirectoryInfo dir in SubDirectories(d))
foreach (FileInfo fileInfo in dir.GetFiles())
yield return fileInfo;
}
public static IEnumerable<FileInfo>
MatchingFilesUnder(Func<FileInfo,boolx, DirectoryInfo d)
{
foreach (FileInfo fileInfo in AllFilesUnder(d))
if (x(fileInfo))
yield return fileInfo;
}
public static IEnumerable<FileInfoFilesByExtension(string ext,
DirectoryInfo d)
{
Func<FileInfo, booltest = x=>x.Extension==ext;
return Match(test, AllFilesUnder(d));
}
public static IEnumerable<ITMatch<IT>(Func<IT, booltest,
IEnumerable<ITcol)
{
foreach (IT it in col)
if(test(it))
yield return (it);
}
static void Main()
{
string testdir = @"C:\Program Files";
List<FileInfofiles =
new List<FileInfo>(FilesByExtension(".txt",
new DirectoryInfo(testdir)));
Console.WriteLine(files.Count);
}
}
--->8---
>I am running C# 3.0 May CTP (LINQ preview) and ReSharper 2.0 (2005)
I am running C# 8.00.50916 on 2.0.50727.

Jul 16 '06 #3
Andrew Matthews <ma*************@gmail.comwrote:
As for compiling code that gives the error - that was what I posted.
The code you posted doesn't have a Main method - how can it compile and
produce the error? Did you try the code I posted? How is it different
from your code? Does the code I post show the error on your machine?

A complete sample is described here:

http://www.yoda.arachsys.com/csharp/complete.html

-- Barry

--
http://barrkel.blogspot.com/
Jul 16 '06 #4
Hello Barry,

I was using NUnit - I didn't need a main method.
My reason for posting the question was to try to find out what it was about
my code that caused the invalid IL.

Andrew
Andrew Matthews <ma*************@gmail.comwrote:
>As for compiling code that gives the error - that was what I posted.
The code you posted doesn't have a Main method - how can it compile
and produce the error? Did you try the code I posted? How is it
different from your code? Does the code I post show the error on your
machine?

A complete sample is described here:

http://www.yoda.arachsys.com/csharp/complete.html

Jul 16 '06 #5
Andrew Matthews <ma*************@gmail.comwrote:
I was using NUnit - I didn't need a main method.
You didn't need a main method, but what about the people you're asking
for help? Do you expect everybody to infer that you are using NUnit,
psychically figure out your version, download it, install it, and modify
your code to execute under NUnit (your code uses namespaces etc.), add
NUnit to the project references, and execute it under NUnit?

Or do you expect them to do what I did - i.e. convert the code into a
complete compiling sample - only to find that it does not perform as you
advertised: it does not cause invalid IL?

I would love help you find a bug is C# 3.0 May release. The trouble is,
you haven't given enough information.
My reason for posting the question was to try to find out what it was about
my code that caused the invalid IL.
The code you provided, to the best of my knowledge, does *not* cause
invalid IL. You'll have to produce a complete, compiling sample if you
are to have any hope of anyone helping you with your problem.

-- Barry

--
http://barrkel.blogspot.com/
Jul 16 '06 #6
Andrew,

LINQ doesn't require a change to the CLR to work. It is most likely an
issue with the compiler (which is to be expected, as this is a CTP, not even
a beta).

Post a complete example (meaning, no nunit, etc, etc) and we can try it
out, and then if it is an error with the compiler, you can post it to the
product feedback center.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andrew Matthews" <ma*************@gmail.comwrote in message
news:ab**************************@news-server.bigpond.net.au...
Hello Barry,

Considering that I wasn't sure whether the problem was with C#
specifically or with the CLR I thought I wouldn't take any chances of
getting a response. As for compiling code that gives the error - that was
what I posted.

Andrew
>Andrew Matthews <ma*************@gmail.comwrote:
>>Hi All,
You have posted to 5 different newsgroups. Don't you think that
.languages.csharp would have sufficed?
>>I get an InvalidProgramException
I have adapted your source code to be a complete, compiling sample. It
does not generate an InvalidProgramException. Can you create a
complete, compiling sample which produces the error?

---8<---
using System;
using System.IO;
using System.Query;
using System.Collections.Generic;
public class App
{
public static IEnumerable<DirectoryInfo>
SubDirectories(DirectoryInfo di)
{
yield return di;
foreach (DirectoryInfo directory in di.GetDirectories())
foreach (DirectoryInfo subDirectory in
SubDirectories(directory))
yield return subDirectory;
}
public static IEnumerable<FileInfoAllFilesUnder(DirectoryInfo d)
{
foreach (DirectoryInfo dir in SubDirectories(d))
foreach (FileInfo fileInfo in dir.GetFiles())
yield return fileInfo;
}
public static IEnumerable<FileInfo>
MatchingFilesUnder(Func<FileInfo,boolx, DirectoryInfo d)
{
foreach (FileInfo fileInfo in AllFilesUnder(d))
if (x(fileInfo))
yield return fileInfo;
}
public static IEnumerable<FileInfoFilesByExtension(string ext,
DirectoryInfo d)
{
Func<FileInfo, booltest = x=>x.Extension==ext;
return Match(test, AllFilesUnder(d));
}
public static IEnumerable<ITMatch<IT>(Func<IT, booltest,
IEnumerable<ITcol)
{
foreach (IT it in col)
if(test(it))
yield return (it);
}
static void Main()
{
string testdir = @"C:\Program Files";
List<FileInfofiles =
new List<FileInfo>(FilesByExtension(".txt",
new DirectoryInfo(testdir)));
Console.WriteLine(files.Count);
}
}
--->8---
>>I am running C# 3.0 May CTP (LINQ preview) and ReSharper 2.0 (2005)
I am running C# 8.00.50916 on 2.0.50727.


Jul 16 '06 #7

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

Similar topics

15
by: Xah Lee | last post by:
Here's the belated Java solution. import java.util.List; import java.util.ArrayList; import java.lang.Math; class math { public static List range(double n) { return range(1,n,1); }
2
by: Patrick Kowalzick | last post by:
Dear NG, I have two containers (standard library) which are nested, e.g.: std::vector< std::vector <int> > A; std::list< std::vector<int> > B; These structures where put in another class...
10
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our...
2
by: Laura K | last post by:
I would like to create a nested datalist inside the selecteditemtemplate of a datalist. My hope is that when a user clicks on a link a set of child links is displayed. For example if the user...
6
by: Matthew Graham | last post by:
Hi, I expect this is very obvious for anyone who knows what they're doing - but I don't understand what's the problem with the following code. I was intending that the program cycle through all...
2
by: Andrew Matthews | last post by:
Hi All, I have an issue that I can't solve with the following (cleaned up) piece of code. What I've seen in the VS 2005 debugger is that the Files iterator is issuing a yield on all of the...
6
by: Andrew Matthews | last post by:
Hi All, I have the following little class of iterators that allow me to iterate over elements in the file system. I have nested some of them, and then added Func<FileInfo, booldelegates to filter...
12
by: Rich Shepard | last post by:
I want to code what would be nested "for" loops in C, but I don't know the most elegant way of doing the same thing in python. So I need to learn how from you folks. Here's what I need to do: build...
19
by: fungus | last post by:
I mentioned earlier to day that I was moving some code from VC++6 to VC++2005 and having trouble with the new iterators. There's all sorts of problems cropping up in the code thanks to this...
11
by: Juha Nieminen | last post by:
Assume we have this: std::list<Typelist1(10, 1), list2(20, 2); std::list<Type>::iterator iter = list1.end(); list1.swap(list2); What happens here, according to the standard? 1) 'iter'...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.