使用对象初始值设定项时如何正确使用断点?
例如,执行以下操作:
foreach (DataRow row in data.Rows)
{
Person newPerson = new Person()
{
Id = row.Field<int>("Id"),
Name = row.Field<string>("Name"),
LastName = row.Field<string>("LastName"),
DateOfBirth = row.Field<DateTime>("DateOfBirth")
};
people.Add(newPerson);
}
将断点设置为单个分配是不可能的,断点设置为整个块。
如果我想具体查看我的代码在哪里被破坏,我必须使用:
foreach (DataRow row in data.Rows)
{
Person newPerson = new Person();
newPerson.Id = row.Field<int>("Id");
newPerson.Name = row.Field<string>("Name");
newPerson.LastName = row.Field<string>("LastName");
newPerson.DateOfBirth = row.Field<DateTime>("DateOfBirth");
people.Add(newPerson);
}
或者也许我遗漏了一些东西。 使用对象初始值设定项时可以正确调试吗?
For example, doing something like this:
foreach (DataRow row in data.Rows)
{
Person newPerson = new Person()
{
Id = row.Field<int>("Id"),
Name = row.Field<string>("Name"),
LastName = row.Field<string>("LastName"),
DateOfBirth = row.Field<DateTime>("DateOfBirth")
};
people.Add(newPerson);
}
Setting a breakpoint to an individual assignation is not possible, the breakpoint is set to the entire block.
If I want to see specifically where my code is breaking, I have to use:
foreach (DataRow row in data.Rows)
{
Person newPerson = new Person();
newPerson.Id = row.Field<int>("Id");
newPerson.Name = row.Field<string>("Name");
newPerson.LastName = row.Field<string>("LastName");
newPerson.DateOfBirth = row.Field<DateTime>("DateOfBirth");
people.Add(newPerson);
}
Or maybe I'm missing something. Can you properly debug when using an object initializer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对象初始值设定项只是语法糖,在编译时会被翻译。您的原始对象初始值设定项变为如下所示:
由于整个块都是这样翻译的,因此您不能在一个步骤中中断。如果您绝对需要在某一特定步骤上中断,您有几种选择。
分手吧。调试时不要使用对象初始值设定项,您可以将它们放回后面。
临时变量。不要直接分配
Id = row.Field("Id")
,而是先将row.Field("Id")
分配给临时变量 (或您想要调试的任何一个),然后将临时变量分配给对象初始值设定项属性。方法调用。您可以将某些代码包装在自定义方法调用中,仅允许您在自定义方法中添加断点。您甚至可以像这样概括它:
Object initializers are just syntactic sugar and get translated when they're compiled. Your original object initializer becomes something like this:
Since the whole block is translated like that you can't break inside one step. If you absolutely need to break on one particular step, you have a few options.
Break it up. Don't use object initializers while debugging, and you can put them back afterwords.
Temp variables. Instead of assigning
Id = row.Field<int>("Id")
directly, assignrow.Field<int>("Id")
to a temp variable first (or whichever one you want to debug) and then assign the temp variable to the object initializer property.Method call. You can wrap some of the code in a custom method call solely to allow you to add a breakpoint within your custom method. You could even generalize it like this:
我在 https 上提出了一个问题2016 年:://developercommunity.visualstudio.com/t/Need-to-be-able-to-add-debugger-break-po/1109823,正在考虑中,但如果社区需要,将会实施。
没有来自社区的赞成票。看起来他们中的大多数人都可以解决问题。
I opened an issue at https://developercommunity.visualstudio.com/t/Need-to-be-able-to-add-debugger-break-po/1109823 in 2016 and it is under consideration but will be implemented if community wants it.
No upvotes from community. Looks like most of them are ok with work-around.