我想以此來加載程序集的程序集:加載DLL中的字節數組
$pathToDll = "C:\zip\SevenZipSharp.dll"
$pathTo7zDll = "C:\zip\7z.dll"
$dllByteArray= [System.IO.File]::ReadAllBytes($pathToDll)
[System.Reflection.Assembly]::Load($dllByteArray)
然而,這並不拋出異常時,我想使用的庫SevenZip.SevenZipExtractor
這樣的:
[SevenZip.SevenZipExtractor]::SetLibraryPath($pathTo7zDll)
它說:Exception calling "SetLibraryPath" with "1" argument(s): "The type initializer for 'SevenZip.SevenZipLibraryManager' threw an exception."
但是如果我有
更換Add-Type -path $pathToDll
它工作正常。
爲什麼它會拋出異常,如果我從字節數組加載程序集?
編輯: 我想用一個字節數組加載它的原因是因爲如果我使用Add-Type
似乎繼續該.dll手柄和後面使用Remove-Item
我不能刪除它。
編輯:這工作:調用[SevenZip.SevenZipExtractor]::SetLibraryPath($pathTo7zDll)
但是爲什麼當我調用[SevenZip.SevenZipExtractor] :: SetLibraryPath($ pathTo7zDll)時拋出Exception? –
我的猜測是被加載的程序集依賴於另一個無法找到的程序集。嘗試首先加載缺失的程序集。您需要深入瞭解該異常的細節 - 查找它的InnerException並檢查它 - 遞歸直到InnerException爲null。 –