Share via

How to fix System.BadImageFormatException

Nicholas Piazza 541 Reputation points
Sep 15, 2021, 5:19 PM

I have a project with a method that uses a DLL from another project. It used to work just fine, but now when I try to execute that method, I get the exception:

System.BadImageFormatException: 'Could not load file or assembly 'MyLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (0x80131058)'

The project file looks like this:
<Project Sdk="Microsft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<LangVersion>9.0</LangVersion>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Reference Include="MyLibrary">
<HintPath>....\MyLibrary\MyLibrary\obj\Debug\net5.0\ref\MyLibrary.dll</HintPath>
</Reference>
</ItemGroup>

</Project>

How do I fix this problem?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,182 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Nicholas Piazza 541 Reputation points
    Sep 15, 2021, 7:38 PM

    OK, Solved the problem. Was using the DLL from .net5.0\ref. Changed to using the DLL from .net5.0 folder. Thanks for the help.

    2 people found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.