装载程序集程序

2023-09-06 09:25:53 作者:此号码已注销

有很多讨论,显示如何从BIN和GAC加载程序集...我的问题是更普遍的,我很想知道如何装配装载工作。

There are plenty of discussing that shows how to load assemblies from BIN and from GAC... my question is more general and I would love to know how assembly loading work.

至于例如:

在bin文件夹,我们可以有

in the BIN folder we can have

A.dll
A.dll.config

A.dll.config文件可以是这样的:

A.dll.config file can look like:

<?xml version="1.0"?>
<configuration>
  <startup>
    <supportedRuntime version="v2.0.50727"/>
  </startup>
</configuration>

要帮助我们设置正确的程序集引用。

to help us setting the correct assembly reference.

我想知道我怎么可以创建一个需要B.DLL,但没有指定任何版本,所以如果总是把B.DLL是在Bin文件夹中A.DLL。

I was wondering how can I create a A.dll that needs B.dll but without specify any version so if would always take the B.dll that is in the BIN Folder.

我的问题是关于SDK和我所有的code仍指向旧的SDK版本的更新,我想有我的组装外观上最全版本的资源,BIN或GAC和使用的一个...

My question is regarding an update of the SDK and all my code still points to the old SDK version, I wanted to have my assembly look for the top most of all versions of the resource, BIN or GAC and use that one...

我怎么能说,在Visual Studio中?我只能添加引用到一个物理文件(版本): - (

How can I say that in Visual Studio? I can only Add Reference to a physical file (version) :-(

推荐答案

如果我理解正确的话,我想你可以用做事情经过的AppDomain对象的插件的方式。通过AppDomain中或许加载程序集,因为你可以设置加载的程序集的路径,你就可以很容易地替换文件,卸载的组件并重新加载新的影拷贝。该影拷贝可以复制最新的文件在当前文件组件的同一位置。然后,建立自己的机制,以检查是否有新的文件,如果存在的话,那么卸载程序集并重新加载最新的。这样一来,一切都将是相当透明的程序,但它需要从你身边多一点编程。不过,你将获得长远发展。

If I understand correctly, I guess you could use the "plugins" way of doing things through the AppDomain object. Perhaps loading the assembly through the AppDomain, since you can set the path for the loaded assembly, you will be able to easily replace the files, unload the assembly and reload the new one in ShadowCopy. The ShadowCopy allows you to copy the latest file at the same location of the current file assembly. Then, build your mechanism to check for a new file, and if it exists, then unload your assembly and reload the latest. This way, everything will be quite transparent for your program, though it requires a bit more programming from your side. Nevertheless, you will gain in long term development.