AssemblyLoadContext 类


表示程序集加载范围的运行时概念。Represents the runtime's concept of a scope for assembly loading.

public ref class AssemblyLoadContext
public ref class AssemblyLoadContext abstract
public class AssemblyLoadContext
public abstract class AssemblyLoadContext
type AssemblyLoadContext = class
Public Class AssemblyLoadContext
Public MustInherit Class AssemblyLoadContext


AssemblyLoadContext表示加载上下文。The AssemblyLoadContext represents a load context. 从概念上讲,加载上下文创建用于加载、解析和可能卸载一组程序集的作用域。Conceptually, a load context creates a scope for loading, resolving, and potentially unloading a set of assemblies.

AssemblyLoadContext 是一个抽象类。AssemblyLoadContext is an abstract class. AssemblyLoadContext.Load(AssemblyName)需要实现方法才能创建具体的类。The AssemblyLoadContext.Load(AssemblyName) method needs to be implemented to create a concrete class.

AssemblyLoadContext存在主要用于提供程序集加载隔离。The AssemblyLoadContext exists primarily to provide assembly loading isolation. 它允许在单个进程中加载同一程序集的多个版本。It allows multiple versions of the same assembly to be loaded within a single process. 它将替换 .NET Framework 中的多个实例提供的隔离机制 AppDomainIt replaces the isolation mechanisms provided by multiple AppDomain instances in the .NET Framework.


AssemblyLoadContext 不提供任何安全功能。AssemblyLoadContext does not provide any security features. 所有代码都具有该进程的完全权限。All code has full permissions of the process.

运行时中的用法Usage in the runtime

运行时实现了两个程序集加载上下文:The runtime implements two assembly load contexts:

应用程序使用情况Application usage

应用程序可以创建自己的 AssemblyLoadContext 来为高级方案创建自定义解决方案。An application can create its own AssemblyLoadContext to create a custom solution for advanced scenarios. 自定义重点介绍如何定义依赖项解析机制。The customization focuses on defining dependency resolution mechanisms.

AssemblyLoadContext提供了两个扩展点来实现托管程序集解析:The AssemblyLoadContext provides two extension points to implement managed assembly resolution:

  1. AssemblyLoadContext.Load(AssemblyName)方法为 AssemblyLoadContext 解析、加载和返回程序集提供了第一种机会。The AssemblyLoadContext.Load(AssemblyName) method provides the first chance for the AssemblyLoadContext to resolve, load, and return the assembly. 如果该 AssemblyLoadContext.Load(AssemblyName) 方法返回 null ,则加载程序尝试将程序集加载到中 AssemblyLoadContext.DefaultIf the AssemblyLoadContext.Load(AssemblyName) method returns null, the loader tries to load the assembly into the AssemblyLoadContext.Default.
  2. 如果 AssemblyLoadContext.Default 无法解析程序集,则原始的 AssemblyLoadContext 另一种机会是解析程序集。If the AssemblyLoadContext.Default is unable to resolve the assembly, the original AssemblyLoadContext gets a second chance to resolve the assembly. 运行时引发 Resolving 事件。The runtime raises the Resolving event.

此外, AssemblyLoadContext.LoadUnmanagedDll(String) 虚拟方法允许自定义默认的非托管程序集解析。Additionally, the AssemblyLoadContext.LoadUnmanagedDll(String) virtual method allows customization of the default unmanaged assembly resolution. 默认实现返回 null ,这会使运行时搜索使用其默认搜索策略。The default implementation returns null, which causes the runtime search to use its default search policy. 在大多数情况下,默认搜索策略就已足够。The default search policy is sufficient for most scenarios.

技术挑战Technical challenges

  • 不能在一个进程中加载多个版本的运行时。It is not possible to load multiple versions of the runtime in a single process.


    加载多个副本或不同版本的 framework 程序集可能导致意外的和难以诊断的行为。Loading multiple copies or different versions of framework assemblies can lead to unexpected and hard-to-diagnose behavior.


    使用具有远程处理或进程间通信的进程边界来解决此隔离问题。Use process boundaries with remoting or interprocess communication to solve this isolation problem.

  • 程序集加载的时间可能会使测试和调试变得困难。The timing of assembly loading can make testing and debugging difficult. 通常会加载程序集,而不会立即解析其依赖项。Assemblies are typically loaded without their dependencies immediately being resolved. 依赖项在需要时加载:The dependencies are loaded as they are needed:

    • 当代码分支到依赖程序集时。When code branches into a dependent assembly.
    • 当代码加载资源时。When code loads resources.
    • 当代码显式加载程序集时。When code explicitly loads assemblies.
  • 的实现 AssemblyLoadContext.Load(AssemblyName) 可添加可能需要隔离以允许不同版本存在的新依赖项。The implementation of AssemblyLoadContext.Load(AssemblyName) can add new dependencies that may need to be isolated to allow different versions to exist. 最自然的实现会将这些依赖项置于默认上下文中。The most natural implementation would place these dependencies in the default context. 仔细设计可以隔离新的依赖项。Careful design can isolate the new dependencies.

  • 同一程序集多次加载到不同的上下文中。The same assembly is loaded multiple times into different contexts.

    • 这可能会导致混淆错误消息,例如 "无法将类型 ' 插件的对象强制转换为类型 ' 插件"。This can lead to confusing error messages, for example "Unable to cast object of type 'Sample.Plugin' to type 'Sample.Plugin'".
    • 跨隔离边界进行的封送处理是非常重要的。Marshaling across isolation boundaries is non-trivial. 典型的解决方案是使用仅加载到默认加载上下文中的程序集中定义的接口。A typical solution is to use an interface defined in an assembly that's only loaded into the default load context.



初始化 AssemblyLoadContext 类的新实例。Initializes a new instance of the AssemblyLoadContext class.


使用一个指示是否启用卸载的值来初始化 AssemblyLoadContext 类的新实例。Initializes a new instance of the AssemblyLoadContext class with a value that indicates whether unloading is enabled.

AssemblyLoadContext(String, Boolean)

使用名称和指示是否启用卸载的值来初始化 AssemblyLoadContext 类的新实例。Initializes a new instance of the AssemblyLoadContext class with a name and a value that indicates whether unloading is enabled.



返回一个包含所有 AssemblyLoadContext 实例的集合。Returns a collection of all AssemblyLoadContext instances.


返回一个集合,它包含在 AssemblyLoadContext 中加载的 Assembly 实例。Returns a collection of the Assembly instances loaded in the AssemblyLoadContext.


获取由最近调用 EnterContextualReflection() 而设置的 AssemblyLoadContextGets the AssemblyLoadContext set by the most recent call to EnterContextualReflection().


获取默认的 AssemblyLoadContextGets the default AssemblyLoadContext. 默认上下文包含主应用程序程序集及其静态依赖项。The default context contains the main application assembly and its static dependencies.


获取一个值,该值指示此 AssemblyLoadContext 是否可回收。Gets a value that indicates whether this AssemblyLoadContext is collectible.


获取 AssemblyLoadContext 的名称。Get the name of the AssemblyLoadContext.



CurrentContextualReflectionContext 设置为 thisAssemblyLoadContextSets CurrentContextualReflectionContext to thisAssemblyLoadContext.


CurrentContextualReflectionContext 设置为加载了程序集的 AssemblyLoadContextSets CurrentContextualReflectionContext to the AssemblyLoadContext which loaded the assembly.


确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)

允许对象在被“垃圾回收”之前尝试释放资源并执行其他清理操作。Allows the object to try to free resources and perform other cleanup operations before it's reclaimed by garbage collection.


获取 AssemblyName 的程序集路径。Gets an AssemblyName for an assembly path.


作为默认哈希函数。Serves as the default hash function.

(继承自 Object)

获取包含指定 AssemblyAssemblyLoadContextGets the AssemblyLoadContext containing the specified Assembly.


获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)

当在派生类中重写时,允许根据其 AssemblyName 解析和加载程序集。When overridden in a derived class, allows an assembly to be resolved and loaded based on its AssemblyName.


根据 AssemblyName 解析并加载程序集。Resolves and loads an assembly given its AssemblyName.


加载指定路径上的程序集文件的内容。Loads the contents of an assembly file on the specified path.

LoadFromNativeImagePath(String, String)

在指定路径上加载托管程序集文件的本机映像的内容。Loads the contents of the native image of a managed assembly file on the specified path.


加载带有基于通用对象文件格式 (COFF) 的映像的程序集,该映像包含托管程序集。Loads the assembly with a common object file format (COFF)-based image containing a managed assembly.

LoadFromStream(Stream, Stream)

加载带有基于通用对象文件格式 (COFF) 的映像的程序集,此映像包含托管程序集,并且还可以选择包括程序集的符号。Loads the assembly with a common object file format (COFF)-based image containing a managed assembly, optionally including symbols for the assembly.


允许派生的类按名称加载非托管库。Allows derived class to load an unmanaged library by name.


从指定路径加载非托管库。Loads an unmanaged library from the specified path.


创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)

设置用于存储此加载上下文的优化配置文件的根路径。Sets the root path where the optimization profiles for this load context are stored.


启动指定配置文件的配置文件优化。Starts the profile optimization for the specified profile.


返回此加载上下文的字符串表示形式。Returns the string representation of this load context.


返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

开始卸载此 AssemblyLoadContextInitiates an unload of this AssemblyLoadContext.



在尝试加载到此程序集加载上下文时,程序集解析失败时发生。Occurs when the resolution of an assembly fails when attempting to load into this assembly load context.


在解析本机库失败时发生。Occurs when the resolution of a native library fails.


卸载 AssemblyLoadContext 时发生。Occurs when the AssemblyLoadContext is unloaded.