使用对象初始值设定项时如何正确使用断点?

发布于 2024-10-19 09:56:34 字数 858 浏览 3 评论 0原文

例如,执行以下操作:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说好的呢 2024-10-26 09:56:34

对象初始值设定项只是语法糖,在编译时会被翻译。您的原始对象初始值设定项变为如下所示:

var temp = new Person();
temp.Id = row.Field<int>("Id");
temp.Name = row.Field<string>("Name");
temp.LastName = row.Field<string>("LastName");
temp.DateOfBirth = row.Field<DateTime>("DateOfBirth");
var person = temp;

由于整个块都是这样翻译的,因此您不能在一个步骤中中断。如果您绝对需要在某一特定步骤上中断,您有几种选择。

  1. 分手吧。调试时不要使用对象初始值设定项,您可以将它们放回后面。

  2. 临时变量。不要直接分配 Id = row.Field("Id"),而是先将 row.Field("Id") 分配给临时变量 (或您想要调试的任何一个),然后将临时变量分配给对象初始值设定项属性。

  3. 方法调用。您可以将某些代码包装在自定义方法调用中,仅允许您在自定义方法中添加断点。您甚至可以像这样概括它:

    Id = BreakThenDoSomething(() => row.Field<int>("Id"));

    public static T BreakThenDoSomething<T>(Func<T> f)
    {
        Debugger.Break();
        return f();
    }

Object initializers are just syntactic sugar and get translated when they're compiled. Your original object initializer becomes something like this:

var temp = new Person();
temp.Id = row.Field<int>("Id");
temp.Name = row.Field<string>("Name");
temp.LastName = row.Field<string>("LastName");
temp.DateOfBirth = row.Field<DateTime>("DateOfBirth");
var person = temp;

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.

  1. Break it up. Don't use object initializers while debugging, and you can put them back afterwords.

  2. Temp variables. Instead of assigning Id = row.Field<int>("Id") directly, assign row.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.

  3. 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:

    Id = BreakThenDoSomething(() => row.Field<int>("Id"));

    public static T BreakThenDoSomething<T>(Func<T> f)
    {
        Debugger.Break();
        return f();
    }
⊕婉儿 2024-10-26 09:56:34

我在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文