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提供實作的 managed 組件解析的兩個擴充點: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

傳回載入至 AssemblyLoadContextAssembly 執行個體的集合。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)

取得組件路徑的 AssemblyNameGets 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)

取得 AssemblyLoadContext,其中包含指定的 AssemblyGets 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 的淺層複本 (Shallow Copy)。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()

起始這個 AssemblyLoadContext 的卸載。Initiates 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.

適用於

另請參閱