与 AppDomain.GetAssemblies()
相比, BuildManager.GetReferencedAssemblies()
( System.Web.Compilation.BuildManager ) 似乎是获取 ASP.NET 应用程序在运行时引用的程序集的更可靠方法,因为 AppDomain.GetAssemblies()仅获取“已加载到此应用程序域的执行上下文中的程序集”。
遍历所有程序集是在应用程序启动时在 DI 容器中动态注册类型的重要工具,尤其是在应用程序启动期间,可能还没有加载其他程序集(在不需要的地方),组合根是第一个需要它们的人。因此,有一个可靠的方法来获取应用程序的引用程序集是非常重要的。
虽然 BuildManager.GetReferencedAssemblies()
是 ASP.NET 应用程序的可靠方法,我想知道:哪些替代品可用于其他类型的应用程序 ,例如桌面应用程序、windows服务和自托管WCF服务?
最佳答案
我目前看到的唯一方法是手动预取所有引用的程序集,就像 BuildManager
在幕后做:
var assemblies =
from file in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory)
where Path.GetExtension(file) == ".dll"
select Assembly.LoadFrom(file);
关于asp.net - BuildManager.GetReferencedAssemblies 等效于非 Web 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12779665/