开发者

Why am I getting a BadImageFormatException when compiling a .NET3.5 project with MSBuild 4.0

I have a project that was created with VS2008. I'm trying to build it with MSBuild 4.0, but still targeting the 3.5 framework. When I do so, I get the exception:

[exec] ResGen : error RG0000: Could not load referenced assembly "C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\PresentationUI\v4.0_4.0.0.0__31bf3856ad364e35\PresentationUI.dll". Caught a BadImageFormatException saying "Could not load file or assembly 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\PresentationUI\v4.0_4.0.0.0__31bf3856ad364e35\PresentationUI.dll' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.".

Note t开发者_如何学编程hat it's trying to load the 4.0 version of PresentationUI.dll, even though I'm compiling for the 3.5 framework. It doesn't matter if I convert the project to VS2010 format or not, the error is the same.


The problem turned out to be that the HintPath of the PresentationUI.dll assembly didn't match the actual assembly location. In that case, MSBuild falls back to looking for the assembly in the GAC. Since MSBuild 4.0 runs on the .NET 4.0 runtime, it picks up the 4.0 version of the assembly, which is incompatible with the 3.5 project being compiled.


I Got the same problem and while looking for the solution in stackoverflow came across this question.Finally after R&D I got the solution.

Remove the old references and add new ones (If any).Go to advanced Compiler option and Select Target Framework as 4.0. :) It worked Enjoy.


One more reason for this Exception is the mismatch of Target Platform among all the Projects in the solution. For example, if a few projects have Target Platform as x86 and the rest have x64 then you will get this exception.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜