Managed assembly loading algorithm

本文内容

Managed assemblies are located and loaded with an algorithm involving various stages.

All managed assemblies except satellite assemblies and WinRT assemblies use the same algorithm.

When are managed assemblies loaded?

The most common mechanism to trigger a managed assembly load is a static assembly reference. These references are inserted by the compiler whenever code uses a type defined in another assembly. These assemblies are loaded (load-by-name) as needed by the runtime.

The direct use of specific APIs will also trigger loads:

APIDescriptionActive AssemblyLoadContext
AssemblyLoadContext.LoadFromAssemblyNameLoad-by-nameThe this instance.
AssemblyLoadContext.LoadFromAssemblyPathAssemblyLoadContext.LoadFromNativeImagePathLoad from path.The this instance.
AssemblyLoadContext.LoadFromStreamLoad from object.The this instance.
Assembly.LoadFileLoad from path in a new AssemblyLoadContext instanceThe new AssemblyLoadContext instance.
Assembly.LoadFromLoad from path in the AssemblyLoadContext.Default instance.Adds a Resolving handler to AssemblyLoadContext.Default. The handler will load the assembly's dependencies from its directory.The AssemblyLoadContext.Default instance.
Assembly.Load(AssemblyName)Assembly.Load(String)Assembly.LoadWithPartialNameLoad-by-name.Inferred from caller.Prefer AssemblyLoadContext methods.
Assembly.Load(Byte[])Assembly.Load(Byte[], Byte[])Load from object.Inferred from caller.Prefer AssemblyLoadContext methods.
Type.GetType(String)Type.GetType(String, Boolean)Type.GetType(String, Boolean, Boolean)Load-by-name.Inferred from caller.Prefer Type.GetType methods with an assemblyResolver argument.
Assembly.GetTypeIf type name describes an assembly qualified generic type, trigger a Load-by-name.Inferred from caller.Prefer Type.GetType when using assembly qualified type names.
Activator.CreateInstance(String, String)Activator.CreateInstance(String, String, Object[])Activator.CreateInstance(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])Load-by-name.Inferred from caller.Prefer Activator.CreateInstance methods taking a Type argument.

Algorithm

The following algorithm describes how the runtime loads a managed assembly.