Today I got a very weird problem which I was able to solve, but I still do not understand why this is happening. This is the scenario:
EDIT
I changed the scenario to be much simpler:
I have a Program that executes the code, and 2 Importers, a base class with a generic type and another class (ImplementingImporter) that just calls the base method and iterates over it.
This is the complete code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IEnumeratorLoadProblem {
class Program {
static void Main(string[] args) {
var importer = new ImplementingImporter();
try {
var data = importer.GetData().ToArray();
} catch (BadImageFormatException ex) {
Console.WriteLine("Why does this fail? " + ex.ToString());
}
Console.WriteLine("Press enter to quit");
Console.ReadLine();
}
}
class BaseClassImporter<T> {
public virtual IEnumerable<T> GetData() {
yield break;
}
}
class ImplementingImporter : BaseClassImporter<int> {
public override IEnumerable<int> GetData() {
// iterating seems to cause the problem
foreach(var dataByBaseImpl in base.GetData()) {
yield return dataByBaseImpl;
}
}
}
}
I get the following error:
System.BadImageFormatException: An attempt was made to load a program
with an incorrect format. (Exception from HRESULT: 0x8007000B)
When I change the code from the used importer to it works:
class ImplementingImporter : BaseClassImporter<int> {
protected override IEnumerable<int> GetData() {
return base.GetData();
}
}
Unfortunately I was not able to look at the generated IL Code, because both ILSpy and Reflector.NET (Version 6) displayed an internal error (I think it was anArgumentOutOfRangeException). I was afraid to use ildasm, so I did not try to look at the IL Code directly.
I guess it has something to do with the generated IL code but I cannot think of the scenario that is causing the Problem.
Any ideas what is happening here?
If the scenario is not clear enough please leave a comment and I will try to make it clearer.
EDIT:
Used .NET version: 4.0. The Application is a ConsoleApplication using VS 2010 SP1.
Build Platform target is AnyCPu, but the problem also shows up when using x86.
My machine has a 64 bit System (Windows 7).
The exception also occurs when using .NET 4.0 client profile.
The example is a single project, no external/unmanaged libraries are used, therefore only the suggested problems (e.g. referencing a 32 bit assembly when running 64 bit) should not occur.
This appears to be a bug with the
yield returnstatement:https://connect.microsoft.com/VisualStudio/feedback/details/677532/an-attempt-was-made-to-load-a-program-with-an-incorrect-format-exception-from-hresult-0x8007000b#details
They state it is fixed in VS2012 (or “the version after VS 2010”).
I am running a console application targeting .NET Framework 4 using VS2010 SP1 and can confirm I get the same error as you. I do not have a VS2012 install available to try this with.
A similar-ish question has been asked here:
Iterator blocks and inheritance
Another suspiciously similar example (this time with async, but triggered on
MoveNextagain):C#5 AsyncCtp BadImageFormatException
Other resources: