.NET唯一的对象标识符 [英] .NET unique object identifier
问题描述
有没有得到一个实例的唯一标识符的方法吗?
GetHash code()
是两个引用指向同一个实例相同。但是,两个不同的实例可以(很容易)得到相同的哈希值code:
Hashtable的哈希codesSeen =新的Hashtable();
LinkedList的<对象> L =新的LinkedList<对象>();
INT N = 0;
而(真)
{
对象o =新的对象();
//记住对象,使他们没有得到收集。
//这没有任何区别,虽然:(
l.AddFirst(O);
INT哈希code = o.GetHash code();
Ñ++;
如果(哈希codesSeen.ContainsKey(哈希code)条)
{
//相同的散列code看到了不同的对象两次(n是低至5322)。
Console.WriteLine(哈希code见过两次:+ N +(+哈希code +));
打破;
}
哈希codesSeen.Add(哈希code,NULL);
}
我正在写一个调试插件,我需要获得某种ID为参考这是该程序的运行过程中是独一无二的。
我已经设法实例,这是独一无二的内部地址,直到垃圾收集器(GC)压缩堆(=移动对象=改变地址)。
堆叠式和NBSP;溢出问题的 Default实施Object.GetHash code() 的有亲缘关系。
我应该说更清楚的对象不是我的控制之下,因为我使用调试器API正在调试访问程序中的对象。如果我是在对象的控制权,加入我自己的唯一标识符将是微不足道的。
我想的唯一ID为建设一个哈希表ID - >对象,要能查找已经看到的物体。现在我解决了它是这样的:
建立一个哈希表:'散code' - > (与哈希code =='散code'的对象的列表)
如果找到对象可见(O){
考生=哈希表[o.GetHash code()] //具有相同散列code对象。
如果没有人选,对象是新的
如果有些考生,他们的地址比较o.Address
如果没有地址等于(哈希code只是一个巧合) - GT; o为新
如果某些地址相等,邻已经看到
}
参考的是的对象的唯一标识。我不知道该转换成像串等参考压实过程中会发生变化的任何有价物品的任何方式(如您所见),但每previous值A将改为价值B,所以尽可能安全code而言它仍然是一个唯一的ID。
如果涉及到的对象是你的控制之下,你可以使用弱引用<创建映射/一>(避免preventing垃圾收集)从基准到您选择的ID(GUID,整型,等等)。这将添加一定量的开销和复杂性,但是
Is there any way of getting an unique identifier of an instance?
GetHashCode()
is the same for the two references pointing to the same instance. However, two different instances can (quite easily) get the same hash code:
Hashtable hashCodesSeen = new Hashtable();
LinkedList<object> l = new LinkedList<object>();
int n = 0;
while (true)
{
object o = new object();
// Remember objects so that they don't get collected.
// This does not make any difference though :(
l.AddFirst(o);
int hashCode = o.GetHashCode();
n++;
if (hashCodesSeen.ContainsKey(hashCode))
{
// Same hashCode seen twice for DIFFERENT objects (n is as low as 5322).
Console.WriteLine("Hashcode seen twice: " + n + " (" + hashCode + ")");
break;
}
hashCodesSeen.Add(hashCode, null);
}
I'm writing a debugging addin, and I need to get some kind of ID for a reference which is unique during the run of the program.
I already managed to get internal ADDRESS of the instance, which is unique until the garbage collector (GC) compacts the heap (= moves the objects = changes the addresses).
Stack Overflow question Default implementation for Object.GetHashCode() might be related.
[Edit] I should have stated more clearly that the objects are not under my control as I am accessing objects in a program being debugged using the debugger API. If I was in control of the objects, adding my own unique identifiers would be trivial.
I wanted the unique ID for building a hashtable ID -> object, to be able to lookup already seen objects. For now I solved it like this:
Build a hashtable: 'hashCode' -> (list of objects with hash code == 'hashCode')
Find if object seen(o) {
candidates = hashtable[o.GetHashCode()] // Objects with the same hashCode.
If no candidates, the object is new
If some candidates, compare their addresses to o.Address
If no address is equal (the hash code was just a coincidence) -> o is new
If some address equal, o already seen
}
The reference is the unique identifier for the object. I don't know of any way of converting this into anything like a string etc. The value of the reference will change during compaction (as you've seen), but every previous value A will be changed to value B, so as far as safe code is concerned it's still a unique ID.
If the objects involved are under your control, you could create a mapping using weak references (to avoid preventing garbage collection) from a reference to an ID of your choosing (GUID, integer, whatever). That would add a certain amount of overhead and complexity, however.
这篇关于.NET唯一的对象标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!