在.net 4.0 wpf应用程序中使用.Net 2.0 dll
我正在尝试在面向 .Net 4 Framework 的 WPF 应用程序中添加对 .Net 2.0 DLL 的引用。
我将
添加到 app.config 文件中。 WPF 应用程序构建良好,但在尝试访问 .Net 2.0 DLL 时在运行时收到 BadImageFormatException。
“尝试加载格式不正确的程序”
这适用于新的测试 WPF 项目,但不适用于我的应用程序。我的应用程序使用实体框架和 MEF。这些技术会导致问题吗?
有什么想法吗?
编辑:已解决
根据下面 Alois 的评论,我的主应用程序定位为“任何 CPU”,DLL 定位为 32 位。
不需要
I am trying to add a reference to a .Net 2.0 DLL in a WPF application that is targeted to the .Net 4 Framework.
I added <startup useLegacyV2RuntimeActivationPolicy="true">
to the app.config file. The WPF app builds fine, but gets a BadImageFormatException at Runtime when trying to access the .Net 2.0 DLL.
"An attempt was made to load a program with an incorrect format"
This works with a new test WPF project, but does not work on my app. My app uses Entity Framework and MEF. Could these technologies be causing the issue?
Any ideas?
Edit: Resolved
According to the comment by Alois below, I had my main app targeted to 'Any CPU' and the DLL was targeted to 32-bit.
<startup useLegacyV2RuntimeActivationPolicy="true">
was not required
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您必须使用 useLegacyV2RuntimeActivationPolicy 属性时,您正在使用以 C++/CLI 编写且面向 CLR 版本 2.0.50727 的混合模式程序集。这样的程序集既包含托管代码又包含本机机器代码。在你的情况下,机器代码是 32 位的,你不能在 64 位进程中运行它。这就是异常的含义。需要在 EXE 项目中将平台目标设置为 x86。
When you have to use the useLegacyV2RuntimeActivationPolicy attribute then you are working with a mixed-mode assembly that was written in C++/CLI and targeting version 2.0.50727 of the CLR. Such an assembly contains both managed code and native machine code. That machine code is 32-bit in your case, you can't run it in a 64-bit process. Which is what the exception means. Setting the Platform target to x86 in your EXE project is required.