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

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

问题描述

我来自 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. :)

在 C++ 中,如果我有两个指针,并且我想知道它们是否指向同一事物,我可以查看内存/观察窗口并查看它们的值 - 看看它们是否指向相同的内存空间.

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?

推荐答案

您要查找的是对象 ID.对于调试器中的任何引用类型,您可以右键单击并说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 步 - 右键单击​​并选择制作对象 ID"

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

第 2 步 - 实例现在显示为 1# 后缀.注意:我在这一步没有做任何特别的事情.单击生成对象 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天全站免登陆