为什么System.Type.GetHash code返回的所有实例和类型相同的值? [英] Why does System.Type.GetHashCode return the same value for all instances and types?

查看:167
本文介绍了为什么System.Type.GetHash code返回的所有实例和类型相同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面code生产的46104728输出:

The following code produces the output of 46104728:

using System;

namespace TestApplication
{
    internal static class Program
    {
        private static void Main()
        {
            Type type = typeof(string);
            Console.WriteLine(type.GetHashCode());
            Console.ReadLine();
        }
    }
}

但这样做的:

But so does this:

using System;

namespace TestApplication
{
    internal static class Program
    {
        private static void Main()
        {
            Type type = typeof(Program);
            Console.WriteLine(type.GetHashCode());
            Console.ReadLine();
        }
    }
 }

然而,在 http://ideone.com 它会产生不同的结果对每种类型。此问题已被再现多个系统了。我使用.NET 4.0现在。

Yet on http://ideone.com it produces varying results for each type. This issue has been reproduced on more than one system now. I'm using .NET 4.0 right now.

推荐答案

您已经遇到自己认为是一个问题,但是,如果你看看他们的哈希值codeS的在同一执行的,你会发现,他们是不相同的,而是依靠使用其顺序:

You've run into what you believe to be a problem, however, if you were to look at their hash codes in the same execution you'll find that they're not identical but instead rely on their order of usage:

Console.WriteLine("{0} {1:08X}", typeof(string), typeof(string).GetHashCode());
Console.WriteLine("{0} {1:08X}", typeof(Program), typeof(Program).GetHashCode());
// System.String 02BF8098
// Program 00BB8560

如果我再次运行同样的程序,交换它们的顺序:

If I run that same program again, swapping their order:

Console.WriteLine("{0} {1:08X}", typeof(Program), typeof(Program).GetHashCode());
Console.WriteLine("{0} {1:08X}", typeof(string), typeof(string).GetHashCode());
// Program 02BF8098
// System.String 00BB8560

这是一个非问题在运行时返回的值不违反规则实施 Object.GetHash code

This is a non-issue at runtime as the returned values do not violate the rules for implementing Object.GetHashCode.

但是,正如你指出的这种行为似乎好奇!

But, as you noted this behavior seems curious!

我钻研的来源,发现实施 Type.GetHash code 被蒙骗关闭到 MemberInfo.GetHash code ,这是再次强加给关到 Object.GetHash code 这就要求 RuntimeHelpers.GetHash code(这一点)

I delved into the source and found the implementation of Type.GetHashCode is foisted off onto MemberInfo.GetHashCode, which is again foisted off onto Object.GetHashCode which calls RuntimeHelpers.GetHashCode(this).

正是在这一点上的线索去寒冷,但是,我的假设是该方法的内部工作创造一个新的价值,每个实例映射,基于电话的顺序。

It is at this point that the trail goes cold, however, my assumption is the inner workings of that method creates a new value, mapped per instance, based on the order of calls.

我通过运行在同一code以上与计划的两个实例(添加属性后,识别它们)测试这个假设:

I tested this hypothesis by running the same code above with two instances of Program (after adding a property to identify them):

var b = new Program() { Name = "B" };
var a = new Program() { Name = "A" };
Console.WriteLine("{0} {1:08X}", a.Name, a.GetHashCode());
Console.WriteLine("{0} {1:08X}", b.Name, b.GetHashCode());
// A 02BF8098
// B 00BB8560

因此​​,类不明确覆盖 Object.GetHash code ,实例将被分配设在顺序看似predictable散列值他们称之为 GetHash code

Thus, for classes which do not explicitly override Object.GetHashCode, instances will be assigned a seemingly predictable hash value based on the order in which they call GetHashCode.

更新:的我就去看了一下转子/共享源代码CLI如何处理这种情况,我了解到,默认实现计算和存储散列code在同步块的对象实例,从而保证了散列code为仅生成一次。默认计算这个散列code是微不足道的,并使用每个线程的种子(包装是我的):

Update: I went and looked at how Rotor/Shared Source CLI handles this situation, and I learned that the default implementation calculates and stores a hash code in the sync block for the object instance, thus ensuring the hash code is generated only once. The default computation for this hash code is trivial, and uses a per-thread seed (wrapping is mine):

// ./sscli20/clr/src/vm/threads.h(938)
// Every thread has its own generator for hash codes so that we
// won't get into a situation where two threads consistently give
// out the same hash codes.
// Choice of multiplier guarantees period of 2**32
// - see Knuth Vol 2 p16 (3.2.1.2 Theorem A).

因此​​,如果实际的CLR如下这种实现它似乎看到的对象的哈希code值的任何差异是基于应用程序域和托管线程其中创建的实例

So if the actual CLR follows this implementation it would seem any differences seen in hash code values for objects are based on the AppDomain and Managed Thread which created the instance.

这篇关于为什么System.Type.GetHash code返回的所有实例和类型相同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆