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提供兩個擴充點來執行 managed 元件解析: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.

    • 這可能會導致混淆的錯誤訊息,例如「無法將類型為 Plugin' 的物件轉型為類型 ' Plugin' "。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.


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


取得組件路徑的 AssemblyNameGets an AssemblyName for an assembly path.


做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)

取得 AssemblyLoadContext,其中包含指定的 AssemblyGets 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)

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