Compared to AppDomain.GetAssemblies()
, BuildManager.GetReferencedAssemblies()
(System.Web.Compilation.BuildManager) seems a more reliable way to get th
The only way I currently see is pre-fetching all referenced assemblies manually, just as the BuildManager
does under the covers:
var assemblies =
from file in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory)
where Path.GetExtension(file) == ".dll"
select Assembly.LoadFrom(file);
I've had the same problem. And after doing some research I've still not found a solid answer. The best I've come up with is to combine AppDomain.CurrentDomain.GetAssemblies()
with the AppDomain.AssemblyLoad
event.
In that way I can process all already loaded assemblies while getting notified for all new assemblies (which I then scan).
This solution is based on @steven's answer. But would work in Web, WinForms, Consoles, and Windows Services.
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);