AssemblyLoadContext AssemblyLoadContext AssemblyLoadContext AssemblyLoadContext Class

定义

表示运行时绑定范围的概念。Represents the runtime's concept of a scope for binding. 此类为抽象类。This class is abstract.

public ref class AssemblyLoadContext abstract
public abstract class AssemblyLoadContext
type AssemblyLoadContext = class
Public MustInherit Class AssemblyLoadContext
继承
AssemblyLoadContextAssemblyLoadContextAssemblyLoadContextAssemblyLoadContext

注解

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 binding 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 binding scenarios. 自定义重点介绍如何定义依赖项解析机制。The customization focuses on defining dependency resolution mechanisms.

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

  1. 为解析程序集、加载和AssemblyLoadContext返回程序集提供了第一种机会。AssemblyLoadContext.Load(AssemblyName)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.Default加载到中。If the AssemblyLoadContext.Load(AssemblyName) returns null, the loader will try to load the assembly into the AssemblyLoadContext.Default.
  2. 如果无法解析程序集, 则原始AssemblyLoadContext的另一种机会是解析程序集。 AssemblyLoadContext.DefaultIf 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 binding isolation process creates multiple types with the same type name.

    • 这可能会导致混淆错误消息, 例如 "type Foo. Foo 无法转换为 Foo 类型。"This can lead to confusing error messages, for example "type Foo.Foo cannot be cast to type Foo.Foo."
    • 跨隔离边界进行的封送处理是非常重要的。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() AssemblyLoadContext() AssemblyLoadContext()

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

AssemblyLoadContext(Boolean) AssemblyLoadContext(Boolean) AssemblyLoadContext(Boolean) AssemblyLoadContext(Boolean)

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

AssemblyLoadContext(String, Boolean) AssemblyLoadContext(String, Boolean) AssemblyLoadContext(String, Boolean) 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 All All All

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

Assemblies Assemblies Assemblies Assemblies

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

CurrentContextualReflectionContext CurrentContextualReflectionContext CurrentContextualReflectionContext CurrentContextualReflectionContext

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

Default Default Default Default

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

IsCollectible IsCollectible IsCollectible IsCollectible

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

Name Name Name Name

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

方法

EnterContextualReflection() EnterContextualReflection() EnterContextualReflection() EnterContextualReflection()

CurrentContextualReflectionContext 设置为 thisAssemblyLoadContextSets CurrentContextualReflectionContext to thisAssemblyLoadContext.

EnterContextualReflection(Assembly) EnterContextualReflection(Assembly) EnterContextualReflection(Assembly) EnterContextualReflection(Assembly)

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

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
Finalize() Finalize() Finalize() Finalize()

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

GetAssemblyName(String) GetAssemblyName(String) GetAssemblyName(String) GetAssemblyName(String)

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

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Object)
GetLoadContext(Assembly) GetLoadContext(Assembly) GetLoadContext(Assembly) GetLoadContext(Assembly)

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

GetType() GetType() GetType() GetType()

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

(Inherited from Object)
Load(AssemblyName) Load(AssemblyName) Load(AssemblyName) Load(AssemblyName)

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

LoadFromAssemblyName(AssemblyName) LoadFromAssemblyName(AssemblyName) LoadFromAssemblyName(AssemblyName) LoadFromAssemblyName(AssemblyName)

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

LoadFromAssemblyPath(String) LoadFromAssemblyPath(String) LoadFromAssemblyPath(String) LoadFromAssemblyPath(String)

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

LoadFromNativeImagePath(String, String) LoadFromNativeImagePath(String, String) LoadFromNativeImagePath(String, String) LoadFromNativeImagePath(String, String)

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

LoadFromStream(Stream) LoadFromStream(Stream) LoadFromStream(Stream) LoadFromStream(Stream)

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

LoadFromStream(Stream, Stream) LoadFromStream(Stream, Stream) LoadFromStream(Stream, Stream) 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) LoadUnmanagedDll(String) LoadUnmanagedDll(String) LoadUnmanagedDll(String)

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

LoadUnmanagedDllFromPath(String) LoadUnmanagedDllFromPath(String) LoadUnmanagedDllFromPath(String) LoadUnmanagedDllFromPath(String)

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

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
SetProfileOptimizationRoot(String) SetProfileOptimizationRoot(String) SetProfileOptimizationRoot(String) SetProfileOptimizationRoot(String)

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

StartProfileOptimization(String) StartProfileOptimization(String) StartProfileOptimization(String) StartProfileOptimization(String)

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

ToString() ToString() ToString() ToString()

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

Unload() Unload() Unload() Unload()

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

事件

Resolving Resolving Resolving Resolving

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

ResolvingUnmanagedDll ResolvingUnmanagedDll ResolvingUnmanagedDll ResolvingUnmanagedDll

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

Unloading Unloading Unloading Unloading

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

适用于

另请参阅