Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging Invoked function in .NET

I call function in .NET using Reflection (Method.Invoke). If an error occurs inside this method and exception is raised, debugger doesn't show actual code but stops at Invoke() call. I can retrieve exception information from InnerException but this is very inconvenient, compared to regular debugging with callstack available and so on. Can methods invoked using Method.Invoke be debugged like regular function calls?

like image 886
John Avatar asked May 16 '11 16:05

John


People also ask

How do you Debug a function in C#?

To start debugging, select F5, or choose the Debug Target button in the Standard toolbar, or choose the Start Debugging button in the Debug toolbar, or choose Debug > Start Debugging from the menu bar. The app starts and the debugger runs to the line of code where you set the breakpoint.

How do you Debug a function?

To debug a function which is defined inside another function, single-step through to the end of its definition, and then call debug on its name. If you want to debug a function not starting at the very beginning, use trace(..., at = *) or setBreakpoint .

How do I Debug a .NET application?

Press F5 to run the program in Debug mode. Another way to start debugging is by choosing Debug > Start Debugging from the menu. Enter a string in the console window when the program prompts for a name, and then press Enter . Program execution stops when it reaches the breakpoint and before the Console.


2 Answers

What you're liking hitting is the "Just My Code" feature. The debugger by default limits debugging to code which is deemed authored by the developer. This reduces a lot of noise that would be present in say debugging a WPF or WinForms launch.

In order to have items like the Exception assistant run for all code you should disable Just My Code.

  • Tools -> Options
  • Debugger
  • Uncheck "Enable Just My Code Debugging"
like image 65
JaredPar Avatar answered Oct 13 '22 02:10

JaredPar


If all else fails, you can also put something like this into the method that gets invoked:

#if DEBUG
    System.Diagnostics.Debugger.Break();
#endif

That will basically "programatically" cause a breakpoint there.

You could also make it slightly better by doing:

  if(Debugger.IsAttached)
    Debugger.Break();

So that it will only break if you are actually running it in the debugger.


Edit:

On 2nd thought, if you have the code to edit it, then you can probably just stick a regular breakpoint there through VisualStudio. I guess my answer doesn't make sense... sorry :)

like image 37
CodingWithSpike Avatar answered Oct 13 '22 01:10

CodingWithSpike