有没有办法强制将所有引用的程序集加载到应用程序域中? [英] Is there a way to force all referenced assemblies to be loaded into the app domain?

查看:26
本文介绍了有没有办法强制将所有引用的程序集加载到应用程序域中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目是这样设置的:

My projects are set up like this:

  • 项目定义"
  • 实施"项目
  • 消费者"项目

项目消费者"同时引用定义"和实现",但没有静态引用实现"中的任何类型.

Project "Consumer" references both "Definition" and "Implementation", but does not statically reference any types in "Implementation".

应用启动时,ProjectConsumer"调用Definition"中的静态方法,需要在Implementation"中查找类型

When the application starts, Project "Consumer" calls a static method in "Definition", which needs to find types in "Implementation"

有没有一种方法可以在不知道路径或名称的情况下强制将任何引用的程序集加载到应用程序域中,最好无需使用成熟的 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))));

正如 Jon 所指出的,理想的解决方案需要递归到每个加载的程序集的依赖项中,但在我的特定场景中,我不必担心.

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.

更新: .NET 4 中包含的托管可扩展性框架 (System.ComponentModel) 具有更好的工具来完成此类工作.

Update: The Managed Extensibility Framework (System.ComponentModel) included in .NET 4 has much better facilities for accomplishing things like this.

这篇关于有没有办法强制将所有引用的程序集加载到应用程序域中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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