【问题标题】:accessing .net2.0 dll in a .net 4.0 dll在 .net 4.0 dll 中访问 .net2.0 dll
【发布时间】:2011-10-28 07:46:44
【问题描述】:

我想在我的 .Net4.0 dll 中加载一个 .NET 2.0 dll。我确实搜索了解决方案,并找到了可以添加到我的 app.config 中的“useLegacyV2RuntimeActivationPolicy”属性,如下所示:Mixed mode assembly is built against version 'v1.1.4322'

但对我来说问题是,就我而言,我试图在我的 4.0 DLL 中使用 .net2.0 dll,而不是在 4.0 exe 中使用。原因是这个 4.0 dll 然后在 Excel 中用作自动化加载项。

目前,当我从 Excel 访问我的 .NET 4.0 API(它为某个功能加载 2.0 dll)时,我得到“混合模式 2.0 dll 无法在 4.0 运行时加载”,这是可以理解的,因为 COM 已经加载了 4.0 CLR,因此无法加载 2.0 dll。

不知何故,我需要制作我的 4.0 DLL 来加载 2.0 运行时。我尝试将 app.config 添加到 4.0 dll,但我猜默认情况下只有 .exe 读取 app.config 以加载 clr。 这是我在我的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>

如何解决这个问题以使我的 .NET 4.0 DLL 能够使用 .NET 2.0 dll?

谢谢, 玛尼

【问题讨论】:

  • 您是否尝试过像添加普通 dll 一样添加 .net 2 dll?
  • 听起来它不是 .NET 2.0 DLL - 它是 .NET 1.1 DLL。
  • .config文件是好的,但是需要将excel.exe.config复制到excel所在的Office目录下。

标签: c# .net com clr com-interop


【解决方案1】:

Hans Passant 已经给出了答案,但他已将其作为评论发布。因此,在这里单独回答这个问题。

该配置必须被称为 excel.exe.config 并且应该与 excel.exe 位于同一办公室目录中才能使此方案正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多