AssemblyLoadContext 类

定义

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

public ref class AssemblyLoadContext abstract
public abstract class AssemblyLoadContext
type AssemblyLoadContext = class
Public MustInherit Class AssemblyLoadContext
继承
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) 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 中多个 AppDomain 实例提供的隔离机制。It 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) provides the first chance for the AssemblyLoadContext to resolve the assembly, load it and return it. 如果 AssemblyLoadContext.Load(AssemblyName) 返回 null,加载程序将尝试将程序集加载到 AssemblyLoadContext.DefaultIf the AssemblyLoadContext.Load(AssemblyName) returns null, the loader will try 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 which 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 and/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 which 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 would be to use an interface defined in an assembly which is only loaded into the default load context.

构造函数

AssemblyLoadContext()

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

AssemblyLoadContext(Boolean)

使用一个指示是否启用卸载的值来初始化 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.

属性

All

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

Assemblies

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

CurrentContextualReflectionContext

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

Default

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

IsCollectible

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

Name

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

方法

EnterContextualReflection()

CurrentContextualReflectionContext 设置为 thisAssemblyLoadContextSets CurrentContextualReflectionContext to thisAssemblyLoadContext.

EnterContextualReflection(Assembly)

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

Equals(Object)

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

(继承自 Object)
Finalize()

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

GetAssemblyName(String)

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

GetHashCode()

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

(继承自 Object)
GetLoadContext(Assembly)

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

GetType()

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

(继承自 Object)
Load(AssemblyName)

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

LoadFromAssemblyName(AssemblyName)

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

LoadFromAssemblyPath(String)

加载指定路径上的程序集文件的内容。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.

LoadFromStream(Stream)

加载带有基于通用对象文件格式 (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.

LoadUnmanagedDll(String)

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

LoadUnmanagedDllFromPath(String)

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

MemberwiseClone()

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

(继承自 Object)
SetProfileOptimizationRoot(String)

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

StartProfileOptimization(String)

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

ToString()

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

Unload()

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

事件

Resolving

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

ResolvingUnmanagedDll

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

Unloading

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

适用于

另请参阅