# Friday, November 1, 2002
« Dependencies | Main | Conference »
GetHashCode & reflection

What is the output of the following code fragment?

  object o = "foo";
  Console.WriteLine(o.GetHashCode());
  Console.WriteLine(typeof(object).GetMethod("GetHashCode").Invoke(o, null));
  Console.WriteLine(typeof(string).GetMethod("GetHashCode").Invoke(o, null));

.NET 1.0 says:

193410979
23
193410979

.NET 1.1 beta says:

193410979
193410979
193410979

Mono 0.13 says:

101574
1554225471
1554225471

Mono 0.16 says:

101574
33369132
101574

Note: the actual numbers are irrelevant, it matters if they match up or not.

.NET 1.1 is correct, the others aren't. The funny thing is, I just realised that IK.VM.NET currently depends (for its System.identityHashCode implementation) on the broken behavior.

.NET 1.0 only behaves this way for methods in String, for other types reflective method invocation works the same as a virtual method call.

In Mono 0.16 all reflective method invocations appear to be non-virtual.

Friday, November 1, 2002 2:41:12 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [5]
Saturday, November 2, 2002 10:06:12 AM (W. Europe Standard Time, UTC+01:00)

I have been tracking your project for a while, and it is very interesting!


Do you have an email address I can mail you directly to?


Miguel

Miguel de Icaza
Saturday, November 2, 2002 10:10:04 AM (W. Europe Standard Time, UTC+01:00)

Oh, we are tracking this bug, could you provide your thougths about why the .NET version is correct here:


http://bugzilla.ximian.com/show_bug.cgi?id=33183

Miguel de Icaza
Saturday, November 2, 2002 10:15:34 AM (W. Europe Standard Time, UTC+01:00)

Actually, can you tell us what the output of this program is with .NET 1.1:


using System;
class X {
static void Main ()
{
object o = new Y ();
Console.WriteLine(o.GetHashCode());
Console.WriteLine(typeof(object).GetMethod("GetHashCode").Invoke(o, null));
Console.WriteLine(typeof(Y).GetMethod("GetHashCode").Invoke(o, null));
}
}


class Y {
public override int GetHashCode ()
{
return 1;
}

}

Miguel de Icaza
Sunday, November 3, 2002 9:26:13 AM (W. Europe Standard Time, UTC+01:00)

This has now been fixed on Mono CVS. Check out the url above for more details
on Invoke.b
Miguel de Icaza
Sunday, November 3, 2002 9:57:11 AM (W. Europe Standard Time, UTC+01:00)

Thanks!
Comments are closed.