简体   繁体   English

在 .net 4.0 dll 中访问 .net2.0 dll

[英]accessing .net2.0 dll in a .net 4.0 dll

I want to load a .NET 2.0 dll in my.Net4.0 dll .我想在 my.Net4.0 dll 中加载 .NET 2.0 dll I did search for the solution and found the 'useLegacyV2RuntimeActivationPolicy' attribute that I can add to my app.config as in this url: Mixed mode assembly is built against version 'v1.1.4322'我确实搜索了解决方案并找到了可以添加到我的 app.config 中的“useLegacyV2RuntimeActivationPolicy”属性,如 url: 混合模式程序集是针对版本“v1.1.4322”构建的

But the problem for me is that, in my case, I am trying to use the .net2.0 dll in my 4.0 DLL and not in an 4.0 exe .但对我来说问题是,就我而言,我试图在我的4.0 DLL 中使用 .net2.0 dll 而不是在 4.0 exe中。 The reason is this 4.0 dll is then used in Excel as an automation add-in.原因是这个 4.0 dll 然后在 Excel 中用作自动化插件。

Currently, when I acccess my .NET 4.0 API (which loads the 2.0 dll for a feature ) from Excel, I get the 'mixed mode 2.0 dll cannot be loaded in 4.0 runtime' which is understandable since COM has loaded the 4.0 CLR and hence cannot load the 2.0 dll. Currently, when I acccess my .NET 4.0 API (which loads the 2.0 dll for a feature ) from Excel, I get the 'mixed mode 2.0 dll cannot be loaded in 4.0 runtime' which is understandable since COM has loaded the 4.0 CLR and hence cannot加载 2.0 dll。

Somehow I need to make my 4.0 DLL to load the 2.0 runtime.不知何故,我需要让我的 4.0 DLL 加载 2.0 运行时。 I tried adding a app.config to the 4.0 dll but I guess only.exe reads the app.config by default to load the clr.我尝试将 app.config 添加到 4.0 dll 但我猜只有.exe 默认读取 app.config 以加载 clr。 This is what I have in my app.config in the project of 4.0 dll but no use.这就是我在 4.0 dll 项目中的 app.config 中的内容,但没有用。

    <?xml version="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <runtime>
    <NetFx40_LegacySecurityPolicy enabled="true"/>
  </runtime>
</configuration>

How do I solve this problem to enable my .NET 4.0 DLL to use .NET 2.0 dll?如何解决此问题以使我的.NET 4.0 DLL能够使用 .NET 2.0 Z06416233FE5EC4C591Z31228?

Thanks, Mani谢谢,玛尼

The answer is already done by Hans Passant but he has posted as a comment.答案已经由 Hans Passant 完成,但他已作为评论发布。 Hence, answering this separately in here.因此,在这里单独回答这个问题。

The config has to be called excel.exe.config and should be in the same office directory as excel.exe for this scenario to work.该配置必须名为 excel.exe.config 并且应该与 excel.exe 位于同一办公目录中,才能使此方案正常工作。

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

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