对象的instanceID? [英] An object's instanceID?

查看:79
本文介绍了对象的instanceID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在调试C#时,我经常会错过对象的唯一标识符,就像我从Java(Eclipse)所知道的那样:
在调试过程中,每个对象都有唯一的ID,您可以轻松查看该对象是否保持不变.如果您要确保两个引用确实相同,并且不仅指向您正在研究的对象的克隆,那么这非常重要.

C#中有类似的东西吗?

非常感谢您
Andreas

Hi,

while debugging C# very often I do miss an unique identifier for objects as I have known it from Java (Eclipse):
There each object has a unique ID during debugging and you can easily see if the object remains the same. This is very important if you want to make sure that two references are really the same and not only pointing to a clone of the object you are investigating.

Is there anything similar in C#?

Thank you very much
Andreas

推荐答案

这不像缺少功能.
有充分的理由不添加此类功能.

但是,我了解您对调试的无奈.我认为您需要调整调试方法(请参阅我提供的另一个答案-替代方法).

现在,请不要怪我如果您已经知道我要提供的建议.无论如何:

1)您需要基本使用object.ReferenceEquals.
2)您可以使用System.Runtime.Serialization.ObjectIDGenerator为参考对象创建唯一的ID. (请小心原始对象或枚举对象-将它们装箱,然后您将获得装箱对象的唯一ID,该ID不会使原始类型的对象唯一,但是无论如何都不需要.)
3)您可以为对象开发自己的唯一ID(最好在"#if debug"下);您可以使用GUID,但最好是使用静态锁对象使静态全局整数值成为线程安全的,并在每个相关类的每个构造函数上递增.

请不要犹豫,问是否上述任何一项不清楚,或者您需要进一步的帮助.
It''s not something like missing feature.
There are good reasons for not adding such feature.

However, I understand your frustration about debugging. I think you need to adjust your debugging method (please see another Answer I offer -- alternative method).

Now, please don''t blame me if you already knew what I''m going to advise. Anyway:

1) You need to make a fundamental use of object.ReferenceEquals.
2) You can create a unique ID for a reference object using System.Runtime.Serialization.ObjectIDGenerator. (Be careful with primitive or enumeration objects -- they will be boxed and then you would get a unique ID of boxed object, which does not make a primitive-type object unique, but no one would need this anyway.)
3) You can develop your own unique ID for your objects (better under "#if debug"); you could use GUID, but better be static global integer value made thread-safe using static lock object, increment on each constructor of each of your classes in question.

Please, don''t hesitate to ask if any of the above items is not clear or you need further help.


这里是替代答案:使用新的Visual Studio调试技术.

由于某种原因,很少有开发人员对此有所了解.这是该怎么办:

1)开始调试(重要的是,它之前不会起作用);
2)进入VS主菜单->调试-> Windows->观看-观看1(或者-立即);
3)进行一些调试步骤,直到您有问题的变量被初始化为止,然后将它们添加到Watch(对于Instant,则不需要);
4)在有问题的已初始化变量之一上,选择监视项,打开上下文菜单,然后选择生成对象ID"(稍后可能要执行删除对象ID".
5)观看:有问题的对象将显示您想要的唯一ID.同样,参照相等的对象将显示相同的ID,所有其他对象将不显示任何内容.
6)用其他变量重复(4-5);应该可以发现所有愚蠢的错误和最聪明的错误;
7) 利润!

我在调试器上进行了实验,并且非常确定以这种方式显示的唯一ID与您使用System.Runtime.Serialization.ObjectIDGenerator所获得的ID完全相同(请参阅我的其他答案).

当我说调整调试方法"时,我的意思就是这样.新方法,不是您以前使用Java的方法,而是更好的方法(如果您考虑过的话).
Here is the alternative answer: using new Visual Studio debugging technique.

By some reason, very few developers knew about it. Here is what to do:

1) Start debugging (important, it won''t work before);
2) Bring Main VS Menu -> Debug -> Windows -> Watch - Watch 1 (alternatively -- Immediate);
3) Proceed some debugging steps until your variables in question initialized, add them to Watch (not needed with Immediate);
4) On one of the initialized variables in question select the watch item, bring up the context menu and select "Make Object ID" (you may want to do "Delete Object ID" later.
5) Watch: the object in question will show unique ID you want. Also, referentially equal object will show the same ID, all other objects will not show anything.
6) Repeat (4-5) with other variables; that should spot all the stupid bugs and most clever ones;
7) Profit!

I experimented under debugger and pretty much sure that the unique IDs show in this way are exactly the same you would get using System.Runtime.Serialization.ObjectIDGenerator (please see my other answer).

When I said "adjust your debugging method", I meant exactly this. New way, not that you used to with Java, but better (if you think about it).


我认为这种方法不可用
请参考此链接
http://www.devnewsgroups.net/dotnetframework/t49222- how-get-instance-id-runtime-objects-net.aspx [
I dont think such thing is available
Please refer this link
http://www.devnewsgroups.net/dotnetframework/t49222-how-get-instance-id-runtime-objects-net.aspx[^]


这篇关于对象的instanceID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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