动态加载不同的组件是彼此的副本 [英] Dynamically loading different assemblies that are copies of each other

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

问题描述

我已经写了一个可插拔的应用程序加载的插件,组装。每个插件读取基本上都有不同的字符串设置一个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.

我知道这是因为该插件接口有一个叫做应用程序名称属性和值从在相应的插件在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.

所以,你加载程序集的第一个副本,同样装配的所有后续负载,不管路径之后,将返回第一组装。这也意味着,大会。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天全站免登陆