调试 .NET 中调用的函数
我使用反射 (Method.Invoke) 调用 .NET 中的函数。如果此方法内发生错误并引发异常,调试器不会显示实际代码,而是在 Invoke() 调用处停止。我可以从 InnerException 检索异常信息,但这与使用可用调用堆栈等进行常规调试相比非常不方便。使用 Method.Invoke 调用的方法可以像常规函数调用一样进行调试吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您喜欢点击的是“Just My Code”功能。默认情况下,调试器将调试限制为被视为由开发人员编写的代码。这减少了调试 WPF 或 WinForms 启动时可能出现的大量噪音。
为了让异常助手等项目针对所有代码运行,您应该禁用“仅我的代码”。
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.
如果所有其他方法都失败了,您还可以将类似的内容放入被调用的方法中:
这基本上会“以编程方式”导致那里出现断点。
您还可以通过执行以下操作使其稍微好一些:
这样,只有当您实际在调试器中运行它时,它才会中断。
编辑:
第二个想法,如果您有编辑它的代码,那么您可能可以通过 VisualStudio 在其中粘贴一个常规断点。我想我的回答没有意义......抱歉:)
If all else fails, you can also put something like this into the method that gets invoked:
That will basically "programatically" cause a breakpoint there.
You could also make it slightly better by doing:
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 :)
我的解决方法是创建计时器并在
Tick()
中调用方法:GUI 代码 ->通过 Method.Invoke 调用函数 ->函数设置一个定时器 ->在 Tick 中有一个函数体
不幸的是它不是很漂亮(并且在某些情况下不适合)
My workaround is to create Timer and call method inside
Tick()
:GUI code -> call function via Method.Invoke -> function setups a Timer -> in Tick there is function body
Unfortunately it's not very pretty (and not suitable in some cases)