You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ibatis.apache.org by Ron Grabowski <ro...@yahoo.com> on 2006/09/28 06:25:53 UTC

IBatisNet - HashCodeProvider.GetIdentityHashCode

public static int GetIdentityHashCode(object obj)
{
 System.Reflection.MethodInfo methodInfo = null;
 Type type = typeof(object);
 methodInfo = type.GetMethod("GetHashCode");
 return (int) methodInfo.Invoke(obj, null);
}

Why can't we call:

public static int GetIdentityHashCode(object obj)
{
 return obj.GetHashCode();
}

Does using reflection call the framework's GetHashCode method even if the object has overriden it?

Can we cache the MethodInfo?

public class HashCodeProvider
{
 private static MethodInfo getHashCodeMethodInfo = null;

 static HashCodeProvider()
 {
  Type type = typeof(object);
  getHashCodeMethodInfo = type.GetMethod("GetHashCode");
 }

 public static int GetIdentityHashCode(object obj)
 {
  return (int)getHashCodeMethodInfo.Invoke(obj, null);
 }
}

Re: IBatisNet - HashCodeProvider.GetIdentityHashCode

Posted by Gilles Bayon <ib...@gmail.com>.
On 9/28/06, Ron Grabowski <ro...@yahoo.com> wrote:
> public static int GetIdentityHashCode(object obj)
> {
>  System.Reflection.MethodInfo methodInfo = null;
>  Type type = typeof(object);
>  methodInfo = type.GetMethod("GetHashCode");
>  return (int) methodInfo.Invoke(obj, null);
> }
>
> Why can't we call:
>
> public static int GetIdentityHashCode(object obj)
> {
>  return obj.GetHashCode();
> }
>
> Does using reflection call the framework's GetHashCode method even if the object has overriden it?
>
Yes bug in .NET 1.0

> Can we cache the MethodInfo?
>
Yes, with a cache with object type as key

-- 
Cheers,
Gilles

<a href="http://www.amazon.com/gp/registry/6JCP7AORB0LE">Wish List</a>