子目录中专用程序集的运行时加载

本文关键字:运行时 加载 专用程序集 子目录 | 更新日期: 2023-09-27 18:27:31

我一直在尝试加载一个私有程序集,该程序集位于应用程序基本目录下的子目录中。我有一个名为Sparrow.dll的程序集,它位于插件目录下(也在应用程序库目录下)。每当我调用Assembly.Load("Sparrow")时,我都会得到一个System.IO.FileNotFoundException.

我使用了带有标记的app.exe.config,它使用了同一程序集的强命名版本,如下所示;

Assembly assem = Assembly.Load("Sparrow");

但是,当我将中的程序集更改为弱程序集时,它不起作用。

配置文件的内容如下;

<configuration>
    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <dependentAssembly name="Sparrow, Culture=neutral, PublicKeyToken=xxxxxx">
            <codeBase version="1.0.1.1" href="plugins/Sparrow.dll" />
         </dependentAssembly>
      </assemblyBinding>
   </runtime>
</configuration>

我读了很多东西,但我不确定使用标记来定位弱程序集是否是一种好的做法。

子目录中专用程序集的运行时加载

您可以将probing元素用于此目的:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="plugins" />
    </assemblyBinding>
  </runtime>
</configuration>

该元素意味着将在plugins子文件夹中搜索程序集。但是,请注意,只有在应用程序目录的派生路径上的目录才能以这种方式指定。

您问题中的配置文件有一个错误。根据文档,XML配置应该如下所示:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
     <dependentAssembly>
       <assemblyIdentity name="Sparrow"
                          publicKeyToken="null"
                          culture="neutral" />
       <codeBase version="1.0.1.1" href="plugins/Sparrow.dll" />
     </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

但是,我认为在您的情况下,使用probing元素会是一个更好的选择。