如何尝试捕捉未找到的装配

joh*_*hny 8 .net c#

好吧,说我有这样的应用程序:

using System;
using AliensExist; // some DLL which can't be found...
Run Code Online (Sandbox Code Playgroud)

我想的是,如果大会DLL AlienExist无法找到该应用程序将不会返回一个错误 - 而是被"trycatched"这就是说,...:

using System;
try{
using AliensExist; // some DLL which can't be found...
} catch {}
Run Code Online (Sandbox Code Playgroud)

怎么做?我知道use关键字以后不能使用...但我现在太懒了测试它.

10倍!

vcs*_*nes 9

你不能在try/catch中真正做到这一点,但你可以做的是AssemblyResolve在AppDomain上处理事件.

有关更多详细信息,请参阅http://msdn.microsoft.com/en-us/library/system.appdomain.assemblyresolve.aspx.

但是,您至少需要编译代码.如果您试图"引用"可能存在或可能不存在的程序集,则需要动态加载它并从那里开始工作.