调试 C# 对象初始值设定项
有没有人有关于调试 C# 对象初始值设定项块中的异常的任何提示? 对象初始值设定项语法基本上是全有或全无,这使得在 LINQ 查询内部进行故障排除变得特别困难。 除了将对象创建分解为单独的方法之外,我可以做些什么来查看哪个属性设置器抛出异常?
Does anyone have any tips for debugging exceptions in a C# object initializer block? The object initializer syntax is basically all or nothing, which can make it especially difficult to troubleshoot inside of a LINQ query. Short of breaking the object creation out to a separate method, is there anything I can do to see which property setter is throwing an exception?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
禁用跳过属性设置器的选项 [跳过属性和运算符(仅限托管)] 可以允许您进入属性设置器。
否则,最好的选择通常是将其分解并在 LINQ 语句之外进行调试。 您可以将初始化参数包装到 linq 中的匿名类型中,并在 linq 语句之外构造对象以进行调试。
Disabling the option to step over property setters [Step over properties and operators (Managed Only)] can allow you to step into the property setters.
Otherwise, the best option is often to just break it out and debug it outside of the LINQ statement. You can wrap your initialization parameters into an anonymous type in linq, and construct your object(s) outside of your linq statement for debugging purposes.
你有没有设置VS在抛出异常时中断? (默认情况下仅在未处理的异常上中断)。 调试| 异常,并检查“抛出”。
仅此一点可能无法回答您的问题,但它可能有助于调试。
除此之外,您可以暂时将代码从初始化程序块中断开(仅用于调试),然后如果需要,您可以在代码正常工作后恢复初始化程序块。
Have you set up VS to break when an exception is thrown? (the default is to break only on unhandled exceptions). Debug | Exceptions, and check "thrown".
That alone is probably not an answer to your question, but it might help with the debugging.
Beyond that, you could temporarily break your code out of the initializer block (just for debugging), and then if you want, you can reinstate the initializer block once you have the code working.
将其从对象初始值设定项块中分离出来,在其中单独设置每个属性。 这样您就可以实际单步执行代码。
Break it out of the object initializer block where your setting each property individually. That way you can actually step into the code.