我在我的ClickOnce部署应用程序中依赖于.NET 2.0 SP2 (方法ApplicationDeployment.CurrentDeployment.CheckForDetailedUpdate(false)
仅限SP2)。
我想在应用程序启动时检查是否存在SP2。我尝试通过捕获调用仅适用于SP2的方法后的MissingMethodException来检测这一点。
/// <summary>
/// The SP2 bootstrapper does not allow HomeSite installation
/// http://msdn.microsoft.com/en-us/vstudio/bb898654.aspx
/// So we only advice the user to download .NET 2.0 SP2 manually.
/// </summary>
private void CheckDotNet2SP()
{
WaitHandle wh = new AutoResetEvent(true);
try
{
wh.WaitOne(1); //this method is .NET 2.0 SP2 only
}
//NOTE: this catch does not catch the MissingMethodException
catch (Exception) //change to catch(MissingMethodException) does not help
{
//report that .NET 2.0 SP2 is missing
}
finally
{
wh.Close();
}
}
如果在没有安装.NET 2.0 SP2的情况下运行此代码,则catch中的代码不会执行。异常只能通过AppDomain.CurrentDomain.UnhandledException
事件处理程序来捕获。
为什么MissingMethodException无法被catch捕获?我可以想象这是一个特殊情况 - CLR遇到了不存在的方法,并且以某种方式无法将其传递到catch块。我想了解背后的原理。
有人对这个问题有任何资源吗?是否还有其他无法在catch块中捕获的异常?