System.BadImageFormatException: Could not load file or assembly 'x_Accessor,...' This assembly is built by a runtime newer

10,654

You can't reference an assembly that is compiled in a higher .NET version (or version of the CLR to be more accurate). So in your example if you have an assembly for unit tests that is .NET 3.5 then you cannot reference an assembly that is .NET 4.0.

This answer is what you are looking for I think:

VS2010 SP1 unit tests targeting 3.5 framework fail if using private accessor

Share:
10,654

Related videos on Youtube

Thalia
Author by

Thalia

Junior developer exploring image processing and driver development

Updated on June 04, 2022

Comments

  • Thalia
    Thalia almost 2 years
    System.BadImageFormatException: Could not load file or assembly 'x_Accessor, 
    Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. 
    This assembly is built by a runtime newer than the currently loaded runtime and 
    cannot be loaded.
    

    I am having a hard time finding a cure for this ailment...

    I have created unit tests for dot net test projects. With the default unit test framework.

    The automated tool created an accessor - which would be used for testing private methods.

    [ I know the theory, private methods should be tested through their callers - but then, it depends on their purpose, content, context and so on... so if I can, I will test them.]

    I have done this in the past on a couple of projects, and it worked fine.

    But on these two projects I am trying to test now, I am running into this problem...

    Note: I am using Framework 3.5 - that may be an issue ? - but the accessor should know that !

    I see absolutely nowhere where I can modify the runtime for the assembly... which I imagine is the accessor.

    And yes, I did clean and rebuild, I did completely erase the output folder and rebuilt, I did delete the accessor and private method and re-create it.

    Please help me find how to fix this ! Thank you.

    • leppie
      leppie over 11 years
      Your automated tool is targeting .NET 4. Use something like ilmerge to retarget.
  • Thalia
    Thalia over 11 years
    The question is - "both ?" Because we are not talking about two projects ! there is only one project, it is dot net 3.5, and... That's it. If you're asking about the test project and the tested project, they are both 3.5, and they are on the same platform - and all tests except for the private one execute fine. I am really not talking about a separate project !!
  • Daniel Lee
    Daniel Lee over 11 years
    What version of Visual Studio are you on?
  • Daniel Lee
    Daniel Lee over 11 years
    If you look under references in your project, are any of the referenced dlls on a higher version of .NET than 3.5?
  • Thalia
    Thalia over 11 years
    They all say v2.0.50727 - which is the value for 3.5
  • Daniel Lee
    Daniel Lee over 11 years
    Have you tried regenerating your private accessors? msdn.microsoft.com/en-us/library/ms184796(v=vs.80).aspx
  • Daniel Lee
    Daniel Lee over 11 years
    Updated my answer, found a link that describes your problem.
  • Thalia
    Thalia over 11 years
    I have deleted the accessor and created a new one (there was no option to "regenerate" it, but I could create it. There was no change, I am still getting the same error. And the project is still 3.5, not newer.
  • Thalia
    Thalia over 11 years
    Thank you, the work-around is soooo complicated, that I decided to give up. While the projects themselves have to be 3.5, I will have to have the tests in 4.0