有没有办法来强制加载到应用程序域的所有引用的程序? [英] Is there a way to force all referenced assemblies to be loaded into the app domain?
问题描述
我的项目都设置这样的:
My projects are set up like this:
- 项目定义
- 项目实施
- 项目消费
项目消费引用了两个定义和实施,但没有静态引用任何类型的实施。
Project "Consumer" references both "Definition" and "Implementation", but does not statically reference any types in "Implementation".
在应用程序启动后,项目消费调用定义一个静态方法,它需要找到类型实施
When the application starts, Project "Consumer" calls a static method in "Definition", which needs to find types in "Implementation"
有没有一种方法,我可以强迫任何引用的程序被加载到应用程序域不知道路径或名称,preferably而无需使用一个完整的IOC框架?
Is there a way I can force any referenced assembly to be loaded into the App Domain without knowing the path or name, and preferably without having to use a full-fledged IOC framework?
推荐答案
这似乎这样的伎俩:
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
var loadedPaths = loadedAssemblies.Select(a => a.Location).ToArray();
var referencedPaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll");
var toLoad = referencedPaths.Where(r => !loadedPaths.Contains(r, StringComparer.InvariantCultureIgnoreCase)).ToList();
toLoad.ForEach(path => loadedAssemblies.Add(AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(path))));
由于乔恩指出,理想的解决方案将需要递归到的依赖关系为每个加载的程序集,但在我的具体情况我不担心了。
As Jon noted, the ideal solution would need to recurse into the dependencies for each of the loaded assemblies, but in my specific scenario I don't have to worry about it.
更新:的托管扩展性框架(System.ComponentModel)包含在.NET 4中都有达到这样的事情很多更好的工具
Update: The Managed Extensibility Framework (System.ComponentModel) included in .NET 4 has much better facilities for accomplishing things like this.
这篇关于有没有办法来强制加载到应用程序域的所有引用的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!