获取继承类的物理文件名 [英] Get physical file name of inherited class

查看:76
本文介绍了获取继承类的物理文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,我不得不问一个与我已经问过并得到回答的问题类似的问题,但我被困在这里,希望有人可以让我再次走上正确的道路。 (此处的原始问题:获取继承类的DLL文件名

I'm sorry to have to ask a similar question to one I've already asked and has been answered but I'm stuck here and was hoping that someone could set me on the right path again. (Original question here: Get DLL's file name of inherited class)

我有一个名为 PluginBase.dll 的抽象类,其他各种类也可以从中继承。这些都被编译为服务器应用程序的不同插件。让我们看一下其中一个名为 PluginMessaging.dll 的插件,其中有一个名为 PluginMessaging.dll.config

I have an abstract class called PluginBase.dll from which various other classes inherit. These are all compiled as different plugins for a server application. Let's look at one of these plugins, called PluginMessaging.dll which has a config file called PluginMessaging.dll.config

读取配置设置的方法在基类中,如下所示:

The method that reads the config settings is in the base class and looks something as follows:

private void ReadConfig()
{
    _runningDir = System.IO.Path.GetDirectoryName(GetType().Assembly.Location);
    _pluginFile = System.IO.Path.GetFileName(GetType().Assembly.Location);
    _configFile = _pluginFile + ".config";

    // Do stuff here that reads from _configFile
}

将代码赋值给 _pluginFile 的代码行是我第一次问这个问题时得到的(由 Damien_The_Unbeliever ),并且只要 PluginMessaging 文件的一个实例和其配置文件。

The line of code that assigns a value to _pluginFile is what I got from the first time I asked the question (answered by Damien_The_Unbeliever) and it works fine, as long as there is only one instance of the PluginMessaging file and its config file.

我现在要做的是制作已编译插件的两个副本,我们称它们为 PluginMessaging_A.dll PluginMessaging_B.dll ,每个都有自己的相应配置文件 PluginMessaging_A.dll.config PluginMessaging_B.dll.config

What I'm trying to do now is to make two copies of the compiled plugin, let's call them PluginMessaging_A.dll and PluginMessaging_B.dll, each with its own corresponding config file, PluginMessaging_A.dll.config and PluginMessaging_B.dll.config respectively.

我遇到的问题是,服务器应用程序会迭代插件文件夹中的所有 *。dll 文件,实例化每个文件,并调用上述 ReadConfig()方法每。当 PluginMessaging_A.dll 调用该函数时,所有操作均按预期方式工作,并且_pluginFile的值为 PluginMessaging_A.dll ,但是当 DLL的第二个副本的函数 PluginMessaging_B.dll 由于某种原因被调用 _pluginFile 再次解析为 PluginMessaging_A.dll

The problem I'm having is that, the server application iterates through all *.dll files in its Plugins folder, instantiates each one and calls the above ReadConfig() method for each. When the function is called for PluginMessaging_A.dll everything works as expected and the value of _pluginFile is PluginMessaging_A.dll but when the ReadConfig() function for the second copy of the DLL, PluginMessaging_B.dll is called, for some reason, _pluginFile again resolves to PluginMessaging_A.dll

似乎内存中有一些表可以记住 GetType()。首次实例化 PluginMessaging.dll 的程序集信息,无论它的物理名称是什么磁盘,并保留该磁盘用于同一DLL的任何后续实例,尽管磁盘上具有不同的名称。

It would seem as if there is some table in memory that remembers the GetType().Assembly information for PluginMessaging.dll the first time it is instantiated, regardless of what physical name it has on disk and retains that for any subsequent instances of the same DLL, albeit with a different name on disk.

推荐答案


似乎在内存中有一些表可以记住GetType()。首次实例化PluginMessaging.dll的汇编信息,无论它在磁盘上的物理名称是什么,对于相同DLL的所有后续实例,即使在磁盘上使用了不同的名称,它都保留该变量。

It would seem as if there is some table in memory that remembers the GetType().Assembly information for PluginMessaging.dll the first time it is instantiated, regardless of what physical name it has on disk and retains that for any subsequent instances of the same DLL, albeit with a different name on disk.

这种观察是正确的。文件名与程序集标识无关。

That observation is correct. The filename is irrelevant for the assembly identity.

认为您要加载两个不同的程序集,但是运行时会确定您要尝试加载相同的程序集再次-并且不会加载它。

You think you're loading two different assemblies, but the runtime determines you're trying to load the same assembly again - and doesn't load it.


MSDN:程序集名称

在确定程序集标识时,运行时不考虑文件名。程序集标识(由程序集名称,版本,区域性和强名称组成)必须在运行时清晰可见。

The runtime does not consider the file name when determining an assembly's identity. The assembly identity, which consists of the assembly name, version, culture, and strong name, must be clear to the runtime.

如果您要从两个文件中加载同一程序集,则需要在单独的appdomain中进行加载。请参阅如何将程序集全部加载到AppDomain递归引用?

If you do want to load the same assembly from two files, you'll need to do so in separate appdomains. See How to Load an Assembly to AppDomain with all references recursively?.

或者,您可以更改名称,版本,区域性或强名称,然后重新编译:这样您将拥有两个不同的版本。

Alternatively you can alter the name, version, culture or strong name and recompile again: then you'll have two different versions.

这篇关于获取继承类的物理文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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