如何使用多个dll版本?

发布于 2024-11-05 05:45:59 字数 298 浏览 0 评论 0原文

好吧,

我这里有一个奇怪的。

我们目前有一个工具应用程序,允许我们创建插件,这些插件只是带有用户控件的 DLL,我只需使用 Assembly.Load 加载并创建新实例,添加到面板即可使用。

然而,插件可能包含第 3 方 dll。

我该如何处理这个问题?

我尝试压缩插件并通过 byte[] 读取程序集,但是在尝试加载时出现错误,缺少依赖的 dll,所以我更进一步,将依赖的 dll 添加到 zip 中并读取它们,同样的错误。

也许我可以创建一个带有插件名称和依赖的 dll 的文件夹?

Alright,

I have a strange one here.

We currently have a tools application that allows us to create plugins, which are just DLLs with usercontrols, that I just load using Assembly.Load and create new instance, add to panel to use.

However a plugin could have 3rd party dlls included.

How can I handle this?

I've tried zipping up the plugin and reading the assembly via byte[], but I get an error when trying to load, missing dependant dll, so I went even farther, added dependant dlls into zip and read them, same error..

Perhaps I can create a folder with plugin name with dependant dlls?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红衣飘飘貌似仙 2024-11-12 05:46:00

我最近有一个项目遇到了完全相同的问题。这是我如何解决的:
- 为每个模块(插件)创建一个单独的目录,在里面我放置了主 module.dll 库和目录 lib,其中放置了所有模块相关的库。
- 在主 Form_Load 事件中,

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(LoadModuleResolveEventHandler);

LoadModuleResolveEventHandler 的位置为:

private Assembly LoadModuleResolveEventHandler(object sender, ResolveEventArgs args)
{
    if (pluginDirectory != null)
    {
        string path = Path.Combine(pluginDirectory , "lib");
        if (Directory.Exists(path))
            foreach (string file in Directory.GetFiles(path))
            {
                if (AssemblyName.GetAssemblyName(file).FullName == args.Name)
                {
                    //Return the loaded assembly.
                    return Assembly.LoadFrom(file);
                }
            }
    }
    return null;
}

它使应用程序在 pluginDirectory(插件模块所在的位置)/lib 子目录中查找引用的 .dll,并且如果找到则将其返回。我在插件代码中做了类似的事情,也许你不必......就是这样。

另一种可能性是向项目添加新项目:应用程序配置文件,并将代码放在下面:

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

它创建 application_name.exe.config 文件,该文件必须与主窗体可执行文件一起部署,并指示在其中查找程序集解析的附加目录。

您可以将这些解决方案中的任何一个放在托盘中,看看它们是如何工作的。
谨致问候,
米杰82

I recently had a project with exactly the same problem. Here is how did I solve it:
- create a separate directory for each module (plugin), inside I put main module.dll library and directory lib, where I put all the module-dependent libraries.
- in main Form_Load event put

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(LoadModuleResolveEventHandler);

where LoadModuleResolveEventHandler is:

private Assembly LoadModuleResolveEventHandler(object sender, ResolveEventArgs args)
{
    if (pluginDirectory != null)
    {
        string path = Path.Combine(pluginDirectory , "lib");
        if (Directory.Exists(path))
            foreach (string file in Directory.GetFiles(path))
            {
                if (AssemblyName.GetAssemblyName(file).FullName == args.Name)
                {
                    //Return the loaded assembly.
                    return Assembly.LoadFrom(file);
                }
            }
    }
    return null;
}

It makes the application to look in pluginDirectory (where the plugin module is) /lib subdirectory, for a referenced .dll, and return it if found. I did similar in plugin's code, maybe you don't have to... That's it.

Other possibility is to add to the project new item: Application Configuration File, and put the code below:

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

It creates application_name.exe.config file which must be deploy with main form executable and indicates addicional directories where to look for assembly resolving.

You can tray any of this solutions and see how they work.
Best regards,
mj82

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文