动态加载互为副本的不同程序集 [英] Dynamically loading different assemblies that are copies of each other

查看:19
本文介绍了动态加载互为副本的不同程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个可插入的应用程序,它将插件加载为程序集.每个插件读取一个 XML 配置文件,该文件基本上具有不同的字符串设置.

I've written a pluggable application that loads plugins as assemblies. Each plugin reads an XML configuration file which basically has different string settings.

使用不同的插件一切正常,但是当我复制和粘贴现有插件程序集 dll(但更改它的 XML 配置)时,我遇到了奇怪的行为.

Everything works fine with different plugins, but I'm experiencing strange behavior when I copy and paste an existing plugin assembly dll (but change it's XML configuration).

Plugin A    |- PluginA.dll
            |- PluginA.xml

Plugin B    |- PluginB.dll
            |- PluginB.xml

加载了原始程序集(A)和复制的程序集(B),但应用程序似乎加载了两次完全相同的插件(B).

The original assembly (A) and copied assembly (B) are loaded, but it seems that that the application has loaded the exact same plugin (B) twice.

我知道这是因为插件接口有一个名为ApplicationName"的属性,并且该值是从相应插件中的 XML 文件中读取的.正在为每个插件正确读取 XML 文件,并且属性值应该分别为A"和B".

I know this because the plugin interface has a property called 'ApplicationName' and the value is read from the XML file in the appropriate plugin. The XML file is being read for each plugin correctly, and the property values are supposed to be 'A' and 'B', respectively.

foreach (var pluginFile in LoadedPluginFiles) // 2 different plugin filenames
    {
       LogMessage("Loading plugin: " + pluginFile); // correct filename in loop
       ObjectHandle oHandle = Activator.CreateInstanceFrom(pluginFile, "MailboxMonitorPlugin.MailboxMonitorPlugin");
       MailboxMonitorPlugin.IMailboxMonitorPlugin pluginInfo = oHandle.Unwrap() as IMailboxMonitorPlugin;
       pluginInfo.Initialize(MailLink.Service.Properties.Settings.Default.PluginsPath);
       LogMessage("Plugin Application Name: " + pluginInfo.ApplicationName.ToString()); // Same application name (B) even though different file loaded in the loop.

加载插件后,我将属性名称写入日志,插件的属性读取了两次.

After I load the plugins, I write the property names out to the log, and the plugin has it's property read twice.

这里是否发生了我不明白的低级操作?也许是指向同一个程序集的指针,因为它们是完全相同的对象?

Is there a low-level operation happening here that I don't understand? Perhaps a pointer to the same assembly because they're exactly the same objects?

推荐答案

您使用 Activator.CreateInstanceFrom 调用.在内部,它将调用 Assembly.LoadFrom.如果您阅读 Assembly.LoadFrom 的文档,您将看到:

You use Activator.CreateInstanceFrom call. Internally, it will call Assembly.LoadFrom. If you read documentation for Assembly.LoadFrom, you will see:

如果已经加载了具有相同标识的程序集,即使指定了不同的路径,LoadFrom 也会返回已加载的程序集.

If an assembly with the same identity is already loaded, LoadFrom returns the loaded assembly even if a different path was specified.

因此,在加载程序集的第一个副本后,同一程序集的所有后续加载,无论路径如何,都将返回第一个程序集.这也意味着 Assembly.CodeBase 属性(您最有可能使用它来获取 xml 配置文件的路径)也将返回第一个加载的程序集的路径,因此是您的问题.

So after you loaded first copy of your assembly, all subsequent loads of the same assembly, no matter the path, will return first assembly. This also means that Assembly.CodeBase property (which you most likely use to get path to your xml configuration file) will return path to the first loaded assembly too, hence your problem.

这篇关于动态加载互为副本的不同程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆