使用通配符或递归进行运行时探测

本文关键字:运行时 探测 递归 通配符 | 更新日期: 2023-09-27 18:18:14

我试图通过<probing>元素加载dll。我有一个文件夹结构,由插件内部的几个插件文件夹组成。因此,我正在寻找一种递归地遍历所有这些插件文件夹以找到dll的方法。

这是我的文件夹结构:
  • MyApplication
    • myapp.exe
  • 插件
    • fooplugin
      • foo.dll
    • barplugin
      • bar.dll

这是我的App.config看起来像,但它似乎没有抓住plugins'*部分。

<?xml version="1.0" encoding="utf-8">
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="MyApplication;plugins;plugins'*;"/>
    </assemblyBinding>
  </runtime>
</configuration>

如果我把它改成:<probing privatePath="MyApplication;plugins;plugins'fooplugin;">,它会找到foo.dll。但我不确定哪些插件会出现

使用通配符或递归进行运行时探测

在使用probing元素时不可能使用通配符搜索。相反,您需要将dll复制到共享文件夹中,或者指定它们可能存在的每个文件夹。

如果你的插件是由其他项目生成的,你可以设置它们直接构建到你的插件文件夹,或者最好在MyApplication项目中有一个后构建任务,将文件复制到你的项目插件文件夹。

您可以通过处理AppDomain.CurrentDomain.AssemblyResolve事件轻松地完成您想要的操作。

然后在附加的事件处理程序中调用:

Assembly assembly = Assembly.LoadFile(path: assemblyPath); return assembly; // return resolved assembly

assemblyPath是你的程序试图抓取的DLL的绝对路径,你可以在运行时扫描你的DLL文件夹,然后抓取包含丢失的DLL名称的路径。