开发者

How to use multiple dll versions?

开发者 https://www.devze.com 2023-03-03 10:53 出处:网络
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

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 depe开发者_如何转开发ndant 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?



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

0

精彩评论

暂无评论...
验证码 换一张
取 消