与AppDomain.GetAssemblies()相比,BuildManager.GetReferencedAssemblies() (System.Web.Compilation.BuildManager)似乎是获取运行时ASP.NET应用程序引用的程序集的一种更可靠的方法,因为AppDomain.GetAssemblies()只获取“已加载到此应用程序域的执行上下文中的程序集”。
迭代所有程序集是在DI容器中的应用程序启动时动态注册类型的重要工具,特别是在应用程序启动期间,其他程序集可能还没有加载(在不需要的地方),并且组合根是第一个需要它们的程序集。因此,有一个可靠的方法来获取应用程序的引用程序集是非常重要的。
虽然BuildManager.GetReferencedAssemblies()是ASP.NET应用程序的可靠方法,但我想知道:,对于其他类型的应用程序(),如桌面应用程序、windows服务和自托管的服务,有什么可供选择的方法?
发布于 2012-10-08 12:11:09
我目前看到的唯一方法是手动预取所有引用的程序集,就像BuildManager在封面下所做的那样:
var assemblies =
from file in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory)
where Path.GetExtension(file) == ".dll"
select Assembly.LoadFrom(file);发布于 2012-10-08 11:41:15
我也遇到过同样的问题。在做了一些研究之后,我仍然没有找到一个确切的答案。我想出的最好方法是将AppDomain.CurrentDomain.GetAssemblies()与AppDomain.AssemblyLoad事件结合起来。
这样,我就可以处理所有已经加载的程序集,同时收到所有新程序集的通知(然后扫描这些程序集)。
发布于 2018-06-01 21:27:43
这个解决方案是基于@steven的回答。但是可以在网络、WinForms、控制台和中工作。
var binDirectory = String.IsNullOrEmpty(AppDomain.CurrentDomain.RelativeSearchPath) ? AppDomain.CurrentDomain.BaseDirectory : AppDomain.CurrentDomain.RelativeSearchPath;
var assemblies = from file in Directory.GetFiles(binDirectory)
where Path.GetExtension(file) == ".dll"
select Assembly.LoadFrom(file);https://stackoverflow.com/questions/12779665
复制相似问题