简体   繁体   English

加载程序集及其依赖项

[英]Loading assemblies and its dependencies

My application dynamically loads assemblies at runtime from specific subfolders. 我的应用程序在运行时从特定子文件夹动态加载程序集。 These assemblies are compiled with dependencies to other assemblies. 这些程序集编译时依赖于其他程序集。 The runtime trys to load these from the application directory. 运行时尝试从应用程序目录加载这些内容。 But I want to put them into the modules directory. 但我想将它们放入模块目录中。

Is there a way to tell the runtime that the dlls are in a seperate subfolder? 有没有办法告诉运行时dll在一个单独的子文件夹中?

One nice approach I've used lately is to add an event handler for the AppDomain's AssemblyResolve event. 我最近使用的一个很好的方法是为AppDomain的AssemblyResolve事件添加一个事件处理程序。

AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);

Then in the event handler method you can load the assembly that was attempted to be resolved using one of the Assembly.Load, Assembly.LoadFrom overrides and return it from the method. 然后在事件处理程序方法中,您可以使用Assembly.Load,Assembly.LoadFrom覆盖之一加载试图解析的程序集,并从该方法返回它。

EDIT: 编辑:

Based on your additional information I think using the technique above, specifically resolving the references to an assembly yourself is the only real approach that is going to work without restructuring your app. 基于您的其他信息,我认为使用上述技术,特别是自己解决对程序集的引用是唯一真正的方法,无需重组您的应用程序即可运行。 What it gives you is that the location of each and every assembly that the CLR fails to resolve can be determined and loaded by your code at runtime... I've used this in similar situations for both pluggable architectures and for an assembly reference integrity scanning tool. 它给你的是CLR无法解决的每个程序集的位置可以在运行时由代码确定和加载...我在类似的情况下使用它来实现可插拔体系结构和程序集参考完整性扫描工具。

You can use the <probing> element in a manifest file to tell the Runtime to look in different directories for its assembly files. 您可以使用清单文件中的<probing>元素告诉Runtime在其组件文件的不同目录中查找。

http://msdn.microsoft.com/en-us/library/823z9h8w.aspx http://msdn.microsoft.com/en-us/library/823z9h8w.aspx

eg: 例如:

<configuration>
 <runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
     <probing privatePath="bin;bin2\subbin;bin3"/>
  </assemblyBinding>
 </runtime>
</configuration>

You can use the <codeBase> element found in the application configuration file. 您可以使用应用程序配置文件中的<codeBase>元素。 More information on " Locating the Assembly through Codebases or Probing ". 有关“ 通过代码库或探测查找程序集 ”的更多信息。

Well, the loaded assembly doesn't have an application configuration file. 好吧,加载的程序集没有应用程序配置文件。

Well if you know the specific folders at runtime you can use Assembly.LoadFrom . 好吧,如果你在运行时知道特定的文件夹,你可以使用Assembly.LoadFrom

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM