如何在一个应用程序中加载不同版本的相同程序集? [英] How to load same assembly of different version in one application?

查看:0
本文介绍了如何在一个应用程序中加载不同版本的相同程序集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C#编写了Windows窗体应用程序。我需要使用下面的DLL。

Interop.SHDocVw.dll

问题是我需要引用同一程序集的两个不同版本。

我尝试将DLL放在&;尝试添加但不允许的两个不同位置。

按照下面的链接,我尝试在运行时加载两个版本的程序集,但不知何故不确定如何做到这一点。

https://www.codeproject.com/Tips/373589/Load-same-assemblies-with-different-versions

我的app.config是这样的:-

<runtime>
<assemblyBinding  xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Interop.SHDocVw.dll" publicKeyToken="db7cfd3acb5ad44e" />
    <codeBase version="1.1.0" href="...watin-coreInterop.SHDocVw.dll"/>
  </dependentAssembly>

  <dependentAssembly>
    <assemblyIdentity name="Interop.SHDocVw.dll" publicKeyToken="632609b4d040f6b4" />
    <codeBase version="1.3.0" href="...winformsInterop.SHDocVw.dll"/>
  </dependentAssembly>

当前正在引发的异常:-

System.IO.FileLoadException: Could not load file or assembly 'Interop.SHDocVw, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
 File name: 'Interop.SHDocVw, Version=1.1.0.0, Culture=neutral, 
 PublicKeyToken=null'
 at myApp.AppUI.InitializeComponent()

如何解决此问题?

推荐答案

http://geekswithblogs.net/narent/archive/2008/11/11/126940.aspx

1)编辑csproj文件(在解决方案资源管理器中右键单击项目,然后卸载项目,修改csproj文件)

2)在此csproj文件中,添加包含引用标签的ItemGroup: (此处是Office Interop 15和16的示例)

<Reference Include="Microsoft.Office.Interop.Excel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
  <HintPath>C:Program Files (x86)Microsoft Visual StudioSharedVisual Studio Tools for OfficePIAOffice15Microsoft.Office.Interop.Excel.dll</HintPath>
  <Aliases>Test2</Aliases>
  <Private>False</Private>
</Reference>

<Reference Include="Microsoft.Office.Interop.Excel, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
  <HintPath>C:Program Files (x86)Microsoft Office
ootOffice16DCFMicrosoft.Office.Interop.Excel.dll</HintPath>
  <Aliases>Test1</Aliases>
  <Private>False</Private>
</Reference>

如您所见,我已经添加了别名:Test1和Test2

3)然后,重新加载项目,并在.NET代码中,在代码的第一行添加(在任何"使用"之前):

extern alias Test1;
extern alias Test2;

然后,您可以在代码中使用具有两个不同版本的相同程序集:

Test1.Microsoft.Office.Interop.Excel.Application app1;
Test2.Microsoft.Office.Interop.Excel.Application app2;

在来自user203687的评论后更新2019年12月18日(我已经下载了您的示例代码,并使其与下面的app.config加载项一起工作):(添加代码库引用解决了问题)

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="SampleLib" publicKeyToken="2901d9e7607fc6eb" />
    <codeBase version="1.0.0.0" href="YourFullPathToLib-v1SampleLib.dll" />
    <codeBase version="1.5.0.0" href="YourFullPathToLib-v2SampleLib.dll" />
  </dependentAssembly>
 </assemblyBinding>
 </runtime>

这篇关于如何在一个应用程序中加载不同版本的相同程序集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆