打开VisualStudio之后,会弹出错误信息 Exception has been thrown by the target of an invocation. 大概意思就是在调用目标上发生了异常,截图如下:
熟悉我的朋友都知道,我个人喜欢用 procdump 来捕获异常,这里可以根据指定异常的方式获取,参考命令和截图如下:
procdump -w devenv -e 1 -f TargetInvocationException -ma -o D:\testdump\
0:000> !t ThreadCount: 22 UnstartedThread: 0 BackgroundThread: 20 PendingThread: 0 DeadThread: 2 Hosted Runtime: no Lock ID OSID ThreadOBJ State GC Mode GC Alloc Context Domain Count Apt Exception 0 1 2840 000001ac95d47e20 a4228 Preemptive 000001AC97806580:000001AC97807218 000001ac95d3a9d0 2 STA System.Reflection.TargetInvocationException 000001ac97804c28 9 2 31d0 000001ac95da3d60 2b228 Preemptive 000001ACD6F92F00:000001ACD6F94E88 000001ac95d3a9d0 0 MTA (Finalizer) ...从卦中可以清晰的看到确实存在一个异常,接下来使用 pe 来观察异常详情,参考如下:
# 堆代码 duidaima.com 0:000> !pe -nested 000001ac97804c28 Exception object: 000001ac97804c28 Exception type: System.Reflection.TargetInvocationException Message: Exception has been thrown by the target of an invocation. InnerException: System.TypeInitializationException, Use !PrintException 000001ac97804838 to see more. StackTrace (generated): SP IP Function 0000008618CFC6E0 0000000000000000 mscorlib_ni!System.RuntimeFieldHandle.GetValue(System.Reflection.RtFieldInfo, System.Object, System.RuntimeType, System.RuntimeType, Boolean ByRef)+0xffff80024c1d3360 0000008618CFC6E0 00007FFDB2CA9F03 mscorlib_ni!System.Reflection.RtFieldInfo.UnsafeGetValue(System.Object)+0xa3 StackTraceString: <none> HResult: 80131604 0:000> !PrintException /d 000001ac97804838 Exception object: 000001ac97804838 Exception type: System.TypeInitializationException Message: The type initializer for 'System.Windows.Automation.Peers.AutomationPeer' threw an exception. InnerException: System.TypeLoadException, Use !PrintException 000001ac978003e8 to see more. StackTrace (generated): <none> StackTraceString: <none> HResult: 80131534 0:000> !PrintException /d 000001ac978003e8 Exception object: 000001ac978003e8 Exception type: System.TypeLoadException Message: 程序集“PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”中的类型“MS.Internal.Automation.SelectionProviderWrapper”的方法“GetSelection”没有实现。 InnerException: <none> StackTrace (generated): SP IP Function 0000008618CFB520 0000000000000000 PresentationCore!System.Windows.Automation.Peers.AutomationPeer.Initialize()+0x1 0000008618CFB5D0 00007FFD55FDD431 PresentationCore!System.Windows.Automation.Peers.AutomationPeer..cctor()+0x81 StackTraceString: <none> HResult: 80131522从卦中可以清晰的看到,原来是SelectionProviderWrapper 类没有 GetSelection 实现,虽然找到了错误原因,但vs不是我们写的,无法修改源码,这个时候直接拿错误信息去网上找咯。