我想在运行时将我的程序加载到我的程序中,匹配我设置的特定程序集属性。为了确保该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的那些拉出来。 –