我有两个项目,项目a和项目b。projecb是一个控制台应用程序,它依赖于ProjectA。昨天,一切都很好,但今天突然当我运行ProjectB时,我得到了这个:

BadImageFormatException未处理: 无法加载文件或程序集“ProjectA, Version=1.0.0.0, Culture=中性,PublicKeyToken=null”或其依赖项之一。试图加载格式不正确的程序。

两者都只是常规项目,不依赖于任何其他非。网络项目。两者都是完全的。net -没有本机代码,也没有P/Invoke。我有其他项目依赖于ProjectA,仍然工作得很好。

我尝试过的事情:

Make sure both projects are set to "Any CPU," with the build checkbox checked. They are. Make sure both projects are for the same Target Framework (.Net 4.0 Client Profile). Under ProjectB --> References --> ProjectA --> Properties, make sure "Copy Local" is set to "True" _ (I verified that ProjectA.dll is being copied correctly) Clean/Rebuild the solution. I even tried manually deleting the /bin and /obj folders in both projects. Restart Visual Studio. Restart my computer. Check out an entirely new copy of the repository.

但还是得到相同的错误。我不知道我做了什么导致了这一切,也不知道该怎么补救。什么好主意吗?


当前回答

我也有同样的问题。我已经将项目A的“平台目标”(“项目A”(右键单击)->属性->构建->“平台目标”)设置为x86,但保持项目B的“任何CPU”。将项目B设置为“x86”修复了这个问题。

其他回答

这也可以通过在app.config文件中定义多个受支持的框架来实现,并强制应用程序在不同的. net框架中运行,而不是在app.config文件中首先提到的框架。

而且,当您的系统中同时拥有上述两个框架时,它也会触发。

作为一种变通方法,在app.config中调出用于调试的目标框架

如果你试图在。net 4中运行,配置文件应该有类似这样的东西,

<supportedRuntime version="v4.0"/>
<supportedRuntime version="v2.0.50727"/>

如果您试图在VS.中使用MSI安装程序打包一个64位项目,也可能会遇到这个问题(“原因是。MSI文件打包的本机shim是32位可执行文件”)。

详情请点击这里:http://blogs.msdn.com/b/heaths/archive/2006/02/01/64-bit-managed-custom-actions-with-visual-studio.aspx

我也有同样的问题。我已经将项目A的“平台目标”(“项目A”(右键单击)->属性->构建->“平台目标”)设置为x86,但保持项目B的“任何CPU”。将项目B设置为“x86”修复了这个问题。

下面的方法为我解决了这个问题,取消选中“Prefer” 32位”:

我在使用Visual Studio Build Steps通过Visual Studio Online (VSTS) Build构建一个项目时得到了这个。

解决方案是:

删除已有的源文件夹 在平台中为所有Visual Studio build(包括依赖项)显式地设置“Any CPU”(见下面的截图)。 重新运行构建