无法找到引用的.dll时是否可以捕获异常?
例如,我有一个C#项目,引用了第三方dll; 如果找不到该dll,则抛出异常.例外是System.IO.FileNotFoundException,但我无法确定将其捕获的位置.以下代码似乎不起作用:
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        try
        {
          // code goes here
        }
        catch (Exception exc)
        {
            MessageBox.Show(exc.ToString());
        }
    }
}
Jar*_*Par 40
扩展Josh的答案.
.Net中的程序集由CLR按需加载.通常,在使用JIT的方法之前不会尝试组装加载,该方法使用该组件中的类型.
如果您无法在main方法中使用try/catch块捕获程序集加载失败,则可能是因为您在try/catch中使用了程序集中的类型.因此异常发生在main方法实际运行之前.
尝试将main方法中的所有代码放在不同的函数中.然后在try/catch块中调用该函数,您应该看到异常.
Jos*_*rke 33
您可以使用 AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
  Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
    }
如果无法自动找到它,请手动查找该程序集.
| 归档时间: | 
 | 
| 查看次数: | 16621 次 | 
| 最近记录: |