C# 并排装配装载问题

C# 并排装配装载问题,c#,.net,reflection,C#,.net,Reflection,我有一个简单的应用程序,在运行时通过以下代码从2个子文件夹加载两个程序集: Assembly.Load("A, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"); Assembly.Load("B, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"); 目录结构为: 因此,预期荷载如下所示: TheApp.exe -> A.dll -> C.dll (vers

我有一个简单的应用程序,在运行时通过以下代码从2个子文件夹加载两个程序集:

Assembly.Load("A, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
Assembly.Load("B, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
目录结构为:

因此,预期荷载如下所示:

TheApp.exe -> A.dll -> C.dll (version 2.0.0.0)
           -> B.dll -> C.dll (version 1.0.0.0)
请注意,
C.dll
已签名,因此两个版本应并排加载

为了确保应用程序从正确的位置加载程序集,我在应用程序配置文件中添加了以下内容:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="B;A" />
    </assemblyBinding>
  </runtime>
</configuration>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       <probing privatePath="B;A" />
    </assemblyBinding>
    <dependentAssembly>
       <assemblyIdentity name="C" publicKeyToken="93a02044a09d059a" /> 
       <codeBase version="1.0.0.0" href="B/C.dll"/>
       <codeBase version="2.0.0.0" href="A/C.dll"/>
    </dependentAssembly>
  </runtime>
</configuration>
问题:为什么运行时只查看“B”文件夹?为什么不继续在A文件夹中查找共享程序集的正确版本

EDIT1:我添加了
标记,如下所述,我知道我的配置文件中有以下内容:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="B;A" />
    </assemblyBinding>
  </runtime>
</configuration>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       <probing privatePath="B;A" />
    </assemblyBinding>
    <dependentAssembly>
       <assemblyIdentity name="C" publicKeyToken="93a02044a09d059a" /> 
       <codeBase version="1.0.0.0" href="B/C.dll"/>
       <codeBase version="2.0.0.0" href="A/C.dll"/>
    </dependentAssembly>
  </runtime>
</configuration>

然而,问题依然存在

请参阅MSDN页面上关于探测的说明,该说明直接解决了您的问题:

如果目录中有程序集的多个版本,并且希望引用该程序集的特定版本,则必须使用
元素,而不是
元素的
privatePath
属性。如果使用
元素,则运行时在第一次找到与引用的简单程序集名称匹配的程序集时停止探测,无论该程序集是否正确匹配。如果匹配正确,则使用该部件。如果匹配不正确,则探测停止,绑定失败

运行时正在查找2.0.0.0版本,但找到1.0.0.0版本并停止查找

最终解决方案是将配置文件更改为以下内容:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
     <probing privatePath="B;A" />
     <dependentAssembly>
       <assemblyIdentity name="C" publicKeyToken="93a02044a09d059a" /> 
       <codeBase version="1.0.0.0" href="B/C.dll"/>
       <codeBase version="2.0.0.0" href="A/C.dll"/>
     </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>


在我看来,您似乎创建了一个测试项目来隔离问题。你能把它上传到其他人可以玩的地方吗?这很可能导致更快的回答。@DanielHilgarth这确实是测试代码,但我必须做大量清理以删除专有内容:(感谢您的回答,刚刚尝试过。请查看我的编辑。@GETah如果您将C.dll移动到根本无法通过探测搜索的文件夹,会发生什么情况?类似于
C\1.0\C.dll
C\2.0\C.dll
。现在运行时正在查看A和B两个文件夹,但不查看C版本的文件夹,这就是问题所在。)persists@GETah也,元素应该放在元素的内部。你搞定了!谢谢!!我编辑了你的答案以包含最终的解决方案。