通过反射调用类库的 DispatcherUnhandledException
我使用反射加载 WPF MVVM 类库。 我还需要一个异常处理程序,如此处所述。
由于这是托管 WPF 应用程序,因此我无法使用 App.xaml ! 这就是为什么我在加载我的应用程序的类中实现了所有需要的内容,如解释的这里,包括:
Application.Current.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException);
这里的问题是,当我抛出异常时(顺便说一句,来自后台工作线程),它不能很好地工作。 实际上,如果我通过调用 Dispatcher.Invoke 手动抛出 NullReferenceException(以便在 UI 线程中抛出异常),并且当我进入 Current_DispatcherUnhandledException 调试器时,我看到的异常不是 NullReferenceException,而是地狱般的 TargetInitation 异常并显示“调用目标已引发异常”消息。
我发现这个异常可能是由invoke方法抛出的,即通过反射调用WPF dll的方法。
看起来 NullReferenceException 被“WPF 类库调用方法”捕获,在 wpf 应用程序之前......
这让我发疯!
请帮忙!
I Load a WPF MVVM Class Library with reflection.
I also need an exception Handler as described here.
Since this is a Hosted WPF App, I can't use App.xaml !
It's why I implemented all needed in the class wich Load my application, as explained here, including :
Application.Current.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException);
The problem here is that when I throw an Exception (from a backgroundworker thread BTW), It doesn't work well.
In effect, if I manually throw a NullReferenceException by calling the Dispatcher.Invoke (in order to throw the exception in the UI thread), and when I get into the Current_DispatcherUnhandledException debugger, the Exception I see is not NullReferenceException, but the helly TargetInvocation Exception with a "Exception has been thrown by the target of an invocation" message.
I figured out that this Exception is maybe thrown by the invoke method, the one which call the WPF dll by reflection.
It looks like the NullReferenceException is caught by the "WPF class library caller method", before the wpf application...
It's making me go crazy !
Please help !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NullReferenceException 确实被 WPF 框架捕获并包装在 TargetInitationException 中。原始的 NullReferenceException 在 TargetInitationException 的 InnerException 字段中仍然可用。
以下是如何检索原始异常的示例:
The NullReferenceException is indeed caught by the WPF framework and wrapped in a TargetInvocationException. The original NullReferenceException is still available in the InnerException field of the TargetInvocationException.
Here's an example on how to retrieve the original exception: