有没有一种方法可以获得实例的唯一标识符?
GetHashCode()
对于指向同一实例的两个引用是相同的。但是,两个不同的实例可以(很容易地)获得相同的哈希码:
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);
}
我正在编写一个调试插件,需要获取某种在程序运行期间唯一的引用ID。
我已经成功获取了实例的内部地址,在垃圾回收器(GC)压缩堆之前是唯一的(移动对象=更改地址)。
Stack Overflow问题 Default implementation for Object.GetHashCode() 可能与此相关。
由于我是使用调试器API访问程序中的对象,因此这些对象不在我的控制之下。如果我能够控制这些对象,添加自己的唯一标识符将非常简单。
我想要为构建哈希表ID -> object获取唯一的ID,以便查找已经看到的对象。目前我是这样解决的:
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
}
ConditionalWeakTable
依赖于RuntimeHelpers.GetHashCode
和object.ReferenceEquals
来进行其内部操作。这种行为与构建一个使用这两种方法的IEqualityComparer<T>
相同。如果您需要性能,我建议您实际上这样做,因为ConditionalWeakTable
在其所有操作周围都有锁定以使其线程安全。 - atlasteConditionalWeakTable
保存对每个Value
的引用,这些引用只有与相应的Key
在其他地方保持的引用一样强。当一个对象是ConditionalWeakTable
中唯一存在的引用,并且其键不存在时,该对象将自动停止存在。 - supercat