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 exact timing of when the static assembly references are loaded is unspecified. It can vary between runtime versions and it is influenced by optimizations like inlining.
The direct use of specific APIs will also trigger loads:
| API | Description | Active AssemblyLoadContext |
|---|---|---|
| AssemblyLoadContext.LoadFromAssemblyName | Load-by-name |
The this instance. |
| AssemblyLoadContext.LoadFromAssemblyPath | Load from path. | The this instance. |
| AssemblyLoadContext.LoadFromStream | Load from object. | The this instance. |
| Assembly.LoadFile | Load from path in a new AssemblyLoadContext instance | The new AssemblyLoadContext instance. |
| Assembly.LoadFrom | Load 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) | Load-by-name. |
Inferred from caller. Prefer AssemblyLoadContext methods. |
| Assembly.Load(Byte[]) | Load from object in a new AssemblyLoadContext instance. | The new AssemblyLoadContext instance. |
| Type.GetType(String) | Load-by-name. |
Inferred from caller. Prefer Type.GetType methods with an |
| Assembly.GetType | If 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) | 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.
Determine the
activeAssemblyLoadContext.- For a static assembly reference, the
activeAssemblyLoadContext is the instance that loaded the referring assembly. - Preferred APIs make the
activeAssemblyLoadContext explicit. - Other APIs infer the
activeAssemblyLoadContext. For these APIs, the AssemblyLoadContext.CurrentContextualReflectionContext property is used. If its value isnull, then the inferred AssemblyLoadContext instance is used. - See table above.
- For a static assembly reference, the
For the
Load-by-namemethods, theactiveAssemblyLoadContext loads the assembly. In priority order by:Checking its
cache-by-name.Calling the AssemblyLoadContext.Load function.
Checking the AssemblyLoadContext.Default instance's cache and running managed assembly default probing logic.
- If an assembly is newly loaded, a reference is added to the AssemblyLoadContext.Default instance's
cache-by-name.
- If an assembly is newly loaded, a reference is added to the AssemblyLoadContext.Default instance's
Raising the AssemblyLoadContext.Resolving event for the active AssemblyLoadContext.
Raising the AppDomain.AssemblyResolve event.
For the other types of loads, the
activeAssemblyLoadContext loads the assembly. In priority order by:Checking its
cache-by-name.Loading from the specified path or raw assembly object.
- If an assembly is newly loaded, a reference is added to
activeAssemblyLoadContext instance'scache-by-name.
- If an assembly is newly loaded, a reference is added to
In either case, if an assembly is newly loaded, then:
- The AppDomain.AssemblyLoad event is raised.
Feedback
Feedback verzenden en weergeven voor