在调试器中唯一标识参考类型 [英] Uniquely Identifying Reference Types in the Debugger

查看:95
本文介绍了在调试器中唯一标识参考类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自C ++背景,所以如果这是一种非C#的思维方式,我很抱歉,但我只需要知道。 :)

I come from a C++ background, so apologies if this is a non-C# way of thinking, but I just need to know. :)

如果我有两个指针,我想知道如果他们指向相同的东西,我可以在内存/看窗口看看他们值 - 看看他们是否指向相同的内存空间。

In C++ if I have two pointers, and I want to know if they point to the same thing, I can look in the memory/watch window and see their value - to see if they are pointing to the same memory space.

在C#中,我无法找到这些内容。一个具有完全相同值的参考类型实际上可能是完全相同的对象,或者可能是一个截然不同的东西。

In C#, I haven't been able to find something along those lines. One reference type with exactly the same values could in fact be the exact same object, or it could be something wildly different.

有没有办法让我看到这种的C#中的信息?也许某种相当于&操作员的手表窗口或某些这样的?

Is there a way for me to see this kind of information in C#? Perhaps some kind of equivalent to the & operator for the watch window or some such?

推荐答案

您要查找的是对象标识。对于调试器中的任何引用类型,您可以右键单击并说出Make Object ID。这将在调试器中显示该实例时向值列中添加一个#后缀。您也可以将#1,#2等等添加到观察窗口,以便稍后再次查看。

What you're looking for are object id's. For any referenc type in the debugger you can right click and say "Make Object ID". This will add a # suffix to the value column whenever that instance is displayed in the debugger. You can also add #1, #2, etc ... to the watch window to see them again any time later.

步骤0 - 运行此代码

Step 0 - Run this code

static void Main(string[] args)
{
    var x = "a string";
    var y = x;
    System.Diagnostics.Debugger.Break();
}

步骤1 - 右键单击​​并选择创建对象标识

Step 1 - Right Click and select "Make Object Id"

步骤2 - 实例现在显示1#后缀。注意:这一步没有什么特别的。点击Make Object Id之后立即更新行更新,以显示1#后缀,因为它们引用了同一个实例。

Step 2 - Instances now display with the 1# suffix. Note: I did nothing special in this step. Immediately after clicking "Make Object Id" both rows updated to display the 1# suffix since they refer to the same instance.

步骤3 - 随时通过在手表窗口中添加1#查看它们

Step 3 - See them at any time by adding 1# to the watch window

这篇关于在调试器中唯一标识参考类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆