通过反射调用类库的 DispatcherUnhandledException

发布于 2024-11-10 01:42:28 字数 903 浏览 0 评论 0原文

我使用反射加载 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瀟灑尐姊 2024-11-17 01:42:28

NullReferenceException 确实被 WPF 框架捕获并包装在 TargetInitationException 中。原始的 NullReferenceException 在 TargetInitationException 的 InnerException 字段中仍然可用。

以下是如何检索原始异常的示例:

public static void Main()
{
    Dispatcher mainThreadDispatcher = Dispatcher.CurrentDispatcher;

    mainThreadDispatcher.UnhandledException += new DispatcherUnhandledExceptionEventHandler(mainThreadDispatcher_UnhandledException);

    // Setup a thread that throws an exception on the main thread dispatcher.
    Thread t = new Thread(() =>
        {
            mainThreadDispatcher.Invoke(new Action(
                () =>
                {
                    throw new NullReferenceException();
                }));
        });

    t.Start();

    // Start the dispatcher on the main thread.
    Dispatcher.Run();
}

private static void mainThreadDispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
    Exception targetInvocationException = e.Exception; // e.Exception is a TargetInvocationException
    Exception nullReferenceException = e.Exception.InnerException; // e.Exception.InnerException is the NullReferenceException thrown in the Invoke above
}

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:

public static void Main()
{
    Dispatcher mainThreadDispatcher = Dispatcher.CurrentDispatcher;

    mainThreadDispatcher.UnhandledException += new DispatcherUnhandledExceptionEventHandler(mainThreadDispatcher_UnhandledException);

    // Setup a thread that throws an exception on the main thread dispatcher.
    Thread t = new Thread(() =>
        {
            mainThreadDispatcher.Invoke(new Action(
                () =>
                {
                    throw new NullReferenceException();
                }));
        });

    t.Start();

    // Start the dispatcher on the main thread.
    Dispatcher.Run();
}

private static void mainThreadDispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
    Exception targetInvocationException = e.Exception; // e.Exception is a TargetInvocationException
    Exception nullReferenceException = e.Exception.InnerException; // e.Exception.InnerException is the NullReferenceException thrown in the Invoke above
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文