jeudi 3 août 2017

C# Not Breaking At Inner Exception Location When Using MethodInfo.Invoke

I'm sorry if this has been asked before, I've been looking around for a few hours now and I haven't been able to gain any insight on my problem. I'm calling Invoke on a MethodInfo I've gotten for a class:

        MethodInfo mappedMethod =  _boundMethod;
        mappedMethod.Invoke(pAction.Target, pParameters);

The problem I'm having, is that if mappedMethod ends up throwing an exception, the debugger will not break at the point of the error as it would with an unhandled exception in normal circumstances. Instead, the exception is passed up to the Invoke method, and it becomes "Exception has been thrown by the target of an invocation."

What I'm wondering, is if there's any way I can get the debugger to break inside the invoked function at the location of the actual problem. I've tried this:

        try
        {
            mappedMethod.Invoke(pAction.Target, pParameters);
        }
        catch (TargetInvocationException e)
        {
            throw e.InnerException;
        }

Which at least throws the actual exception, but the debugger breaks right at the throw and still will not move the debugger cursor to where the error occurred.

I'm aware that you can make the debugger break on all thrown exceptions immediately with Debug->Exceptions, but that's not appropriate in my situation because I'd have to turn it on and off constantly (My code has try-catches everywhere that throw exceptions for user created content fairly often) and above that, the main purpose of my code is to function as a scripting language for 3rd party users. I'm using Invoke to call user scripts, so it's important that my scripting layer is able to show users where their errors occurred without them needing to change debugger settings.

Is there anything I can do, or is Invoke just not going to be able to work in the way I want it to? Again sorry if this has been asked, it seems like something somebody else would have wanted to know too!





Aucun commentaire:

Enregistrer un commentaire