卸載使用Assembly.LoadFrom() 載入的程式集
為了檢查載入DLL 後執行GetTypes() 所需的時間使用Assembly .LoadFrom(),可能需要卸載並重新載入DLL。但是,將程序集引用設為 null 不足以卸載組件。
明確卸載程序集
要明確卸載程序集,可以使用 AppDomain.Unload () 方法。以下是一個範例:
// Create a separate AppDomain to load the assembly AppDomain dom = AppDomain.CreateDomain("some"); // Load the assembly into the new AppDomain Assembly assembly = dom.LoadFrom(filePath); // Get the types from the assembly Type[] types = assembly.GetTypes(); // Unload the AppDomain, which will unload the assembly AppDomain.Unload(dom);
透過卸載 AppDomain,程式集及其所有資源將被垃圾收集器回收。
使用垃圾收集器
雖然將程式集引用設為 null 不會明確卸載程式集,但它會使其成為垃圾 收藏。垃圾收集器最終會回收分配給 Assembly 物件的資源,並且組件檔案將從記憶體中卸載。
但是,無法明確呼叫垃圾收集器或強制其立即運作。它是一個後台進程,在系統空閒時間時運行。因此,如果您想要明確卸載組件,建議使用 AppDomain.Unload()。
以上是如何明確卸載使用 Assembly.LoadFrom() 載入的組件?的詳細內容。更多資訊請關注PHP中文網其他相關文章!