我想在運行時將我的程序加載到我的程序中,匹配我設置的特定程序集屬性。爲了確保該dll加載之前我檢查其標誌我寫了下面的方法:C#異常沒有被拋出/被捕獲
private bool IsValidDll(string dll) {
try {
System.Reflection.Assembly.LoadFrom(dll);
return true;
} catch (Exception ex) { return false; }
}
我可以遍歷DLL文件在我的當前目錄下,調用該方法,看它是否會是安全的加載DLL並檢查其裝配屬性。但是,我正在碰到一個不會拋出/捕獲異常的DLL,它仍然會直接導致程序崩潰。相關的輸出窗口信息如下:
LoaderException: System.IO.FileLoadException: Mixed mode assembly is built against
version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information. - Adapters.Spryware.SprywareAdapter
System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
我試圖捕捉特定的異常(System.IO.FileLoadException),但catch塊仍然跳過,我仍然崩潰。有什麼想法嗎?
此外,我發現這是檢查我的程序集屬性的一個相當重的方式。有沒有辦法檢查我的國旗,而不必首先加載與反思的DLL?
嘗試'ReflectionOnlyLoad'而不是'LoadFrom'來查看它是否解決了問題。 – dasblinkenlight
我認爲你錯過了DLL當你做IsValidDll的第一次檢查... – Danpe
@dasblinkenlight我試着使用ReflectionOnlyLoad和我現在成功地拋出異常,但每一個dll拋出一個異常,並返回false現在在哪裏,我以前能夠至少要把我知道包含我的Assembly Attribute的那些拉出來。 –