简体   繁体   English

在.net 4.0 wpf应用程序中使用.net 2.0 dll

[英]use .Net 2.0 dll in .net 4.0 wpf application

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. 我试图在针对.Net 4框架的WPF应用程序中添加对.Net 2.0 DLL的引用。

I added <startup useLegacyV2RuntimeActivationPolicy="true"> to the app.config file. 我在app.config文件中添加了<startup useLegacyV2RuntimeActivationPolicy="true"> The WPF app builds fine, but gets a BadImageFormatException at Runtime when trying to access the .Net 2.0 DLL. WPF应用程序构建良好,但是在尝试访问.Net 2.0 DLL时在运行时获取BadImageFormatException。

"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. 这适用于新的测试WPF项目,但不适用于我的应用程序。 My app uses Entity Framework and MEF. 我的应用程序使用实体框架和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. 根据下面的Alois的评论,我的主要应用程序针对“ Any CPU”,而DLL则针对32位。

<startup useLegacyV2RuntimeActivationPolicy="true"> was not required <startup useLegacyV2RuntimeActivationPolicy="true">

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. 当您必须使用useLegacyV2RuntimeActivationPolicy属性时,您正在使用以C ++ / CLI编写且面向CLR版本2.0.50727的混合模式程序集。 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. 在您的情况下,该机器代码是32位的,因此您不能在64位的进程中运行它。 Which is what the exception means. 这就是异常的含义。 Setting the Platform target to x86 in your EXE project is required. 需要在EXE项目中将Platform目标设置为x86。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM