none
System.BadImageFormatException in compiled code. RRS feed

  • Question

  • I have the following code

    using System;
    using System.Collections.Generic;
    
    namespace ConsoleApplication5
    {
    	
    	class Program
    	{
    		class A<T>
    		{
    			protected virtual IEnumerable<T> Boo()
    			{
    				return null;
    			}
    		}
    
    		class B : A<int>
    		{
    			public System.Collections.IEnumerable Foo()
    			{
    				yield return base.Boo();
    			}
    		}
    
    		static void Main(string[] args)
    		{
    			foreach (var x in new B().Foo())
    			{
    				Console.WriteLine(x);
    			}
    		}
    	}
    }
    
    

    It compiled without any error but after start I get this error

    Unhandled Exception: System.BadImageFormatException: An attempt was made to load

     a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

       at ConsoleApplication5.Program.B.<Foo>d__0.MoveNext()

       at ConsoleApplication5.Program.Main(String[] args) in C:\Projects\ConsoleAppl

    ication5\ConsoleApplication5\Program.cs:line 27

     

    Any ideas?

     

    • Moved by Leo Liu - MSFT Monday, July 4, 2011 6:25 AM Moved for better support. (From:Visual C# General)
    Thursday, June 30, 2011 12:10 PM

All replies

  • foreach (var x in new B().Foo())
    Where do you declare you B?

    If you have declare your B then remove the new keyword and see.

    chanmm


    chanmm
    Friday, July 1, 2011 2:19 AM
  • Hi,

    I tested to run the code above, but no exception.

    The Foo method returns null as excepted.

    My test environment is VS2008/.NET Framework 3.5/AnyCPU/64-bit Windows 7.

     


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, July 5, 2011 6:40 AM
  • My enviroment VS2010/.net 4/ Windows XP 32-bit

     

    Also code work if remove "virtual" modificator on Boo function.

    Tuesday, July 5, 2011 6:50 AM
  • I have the following code

     

    using System;
    
    using System.Collections.Generic;
    
    
    
    namespace ConsoleApplication5
    
    {
    
    	
    
    	class Program
    
    	{
    
    		class A<T>
    
    		{
    
    			protected virtual IEnumerable<T> Boo()
    
    			{
    
    				return null;
    
    			}
    
    		}
    
    
    
    		class B : A<int>
    
    		{
    
    			public System.Collections.IEnumerable Foo()
    
    			{
    
    				yield return base.Boo();
    
    			}
    
    		}
    
    
    
    		static void Main(string[] args)
    
    		{
    
    			foreach (var x in new B().Foo())
    
    			{
    
    				Console.WriteLine(x);
    
    			}
    
    		}
    
    	}
    
    }
    
    
    
    

     

    It compiled without any error but after start I get this error

     

    Unhandled Exception: System.BadImageFormatException: An attempt was made to load

     a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

       at ConsoleApplication5.Program.B.<Foo>d__0.MoveNext()

       at ConsoleApplication5.Program.Main(String[] args) in C:\Projects\ConsoleAppl

    ication5\ConsoleApplication5\Program.cs:line 27

     

     

    Any ideas?

     


    I havent tried to run the code, but any clean compile that causes that kind of exception is surely a serious bug in the compiler or CLR, I'd post this as a bug.

     

    Cap'n

     

    Tuesday, July 5, 2011 12:02 PM