On Aug 29, 12:00*am, "jehugalea...@gmail.com" <jehugalea...@gmail.com>
wrote:
Thanks. Any ideas about improving the GetHashCode?
I'm assuming there isn't anything I can do about the GetHashCode. I
can't use memory addresses since they are volitile because of the
memory management.
There isn't anything that can be done in pure C#. However, it is
possible in IL to make non-virtual method calls even for virtual
methods, explicitly specifying the type that should provide the
implementation that is called. In your case, what you need is to make
a non-virtual call to Object.GetHashCode() on your objects - that
would be the default implementation that provides identity-based hash.
So, you take the C# code written along these lines:
using System;
using System.Collections.Generic;
public class IdentityComparer<T: IEqualityComparer<T>
where T : class
{
public bool Equals(T x, T y)
{
return x == y;
}
public int GetHashCode(T x)
{
return x.GetHashCode();
}
}
Compile it, then ildasm it. In the resulting .il file, find this
instruction:
callvirt instance int32 [mscorlib]System.Object::GetHashCode()
replace "callvirt" with "call", and use ilasm to recompile the
assembly. Now you've got your identity comparer with proper
GetHashCode().