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. 它取代了由多个提供的隔离机制AppDomain.NET Framework 中的实例。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.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 binding isolation process creates multiple types with the same type name.

    • 这可能会导致令人混淆的错误消息,例如"Foo.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)
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)
StartProfileOptimization(String) StartProfileOptimization(String) StartProfileOptimization(String) StartProfileOptimization(String)
ToString() ToString() ToString() ToString()

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

(Inherited from Object)
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
Unloading Unloading Unloading Unloading

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

适用于

另请参阅