views:

199

answers:

2

What is .NET Compact Framework equivalent for following method? Is there any P/Invoke call available?

System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(Object)

I am in middle of an open source project port to .NET Compact Framework.

A: 

object exposes GetHashCode, so you could probably do something simple like this:

int GetHashCode(object o)
{
  return o.GetHashCode();
}
ctacke
That won't neccessarily call the one on Object, though. If GetHashCode has been overridden by a descendent class, that's the one that would get called, even if you were to directly cast the instance to Object before making the call.
Mel
+2  A: 

There is no PInvoke call possible for this method. RuntimeHelpers.GetHashCode() really just calls into an internal CLR method (Object.InternalGetHashCode). It's not possible to PInovke into such a function.

This method is really just calling Object.GetHashCode() in a non-virtual way. Unfortunately there is not a way to do this statically. C# does not support calling a method on a given object non-virtually (CLR considers this non-veriable code).

Your best bet is to call into Object.InternalGetHasheCode via reflection. You'll have to check and see if that method is implemented on the Compact Framework though. My expectation is that it will be but I don't have a mscorlib for CF handy.

Documentation for RuntimeHelpers.GetHashCode: http://msdn.microsoft.com/en-us/library/11tbk3h9.aspx

JaredPar