简体   繁体   中英

How to pre load every assemblies in a solution with multiple projects

I have a Solution with 4 Projects and these projects make references each other in this way:

  • Project RoleCreator makes reference to Project Unify.ServiceInterface and Unify.ServiceModel

I want to pre load the assemblies from ALL THE PROJECTS in the solution at some method in the Project RoleCreator, but in this line Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); in the breakpoint value appears load just the Assembly from the projectUnify.ServiceInterface but not the Assembly from the project Unify.ServiceModel that it's also referenced in the project RoleCreator.I want to load all the assemblies from THE SOLUTION!

在此处输入图片说明

You can use such code:

public static void PreloadAssembly(Assembly assembly)
{
    var references = assembly.GetReferencedAssemblies();
    foreach (var assemblyName in references)
    {
        Assembly.Load(assemblyName);
    }
}

And then

PreloadAssembly(typeof(TypeFromAssembly1).Assembly);
PreloadAssembly(typeof(TypeFromAssembly2).Assembly);

This call

AppDomain.CurrentDomain.GetAssemblies()

Will return list of already loaded assemblies. If you didn't access type from some assembly, then AppDomain won't load it, and it won't be present in GetAssemblies call. AppDomain loads assemblies for types that you used.

And as an another way to do it is described here - how to load all assemblies from within your /bin directory . It just loads all assemblies from bin folder

If it is web application, you can use BuildManager.GetReferencedAssemblies . Here is an example from Autofac wiki (while it is not relevant that it comes from Autofac):

var assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>();

That will force the referenced assemblies to be loaded into the AppDomain immediately making them available for module scanning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM