为String.GetHashCode()创建重载以在x86和x64环境中返回相同的值

时间:2023-01-24 16:11:45

I have a scenario in which my server side code runs in x86 environment where as the client side runs in x64. The issue is happening that we are using String.GetHashCode() to identify the objects within the HashTable since "The behavior of GetHashCode is dependent on its implementation, which might change from one version of the common language runtime to another. " -MSDN. I used the unsafe code to create x86 equivalent GetHashCode() from the following stack overflow question: Can I depend on the values of GetHashCode() to be consistent?.

我有一个场景,我的服务器端代码在x86环境中运行,其中客户端在x64中运行。问题是我们正在使用String.GetHashCode()来识别HashTable中的对象,因为“GetHashCode的行为依赖于它的实现,这可能会从公共语言运行库的一个版本变为另一个版本。” - MSDN。我使用不安全的代码从以下堆栈溢出问题创建x86等效的GetHashCode():我可以依赖GetHashCode()的值来保持一致吗?

I had a few questions:

我有几个问题:

  1. Is it recommended to leave unsafe code within managed code world.
  2. 是否建议在托管代码世界中保留不安全的代码。

  3. How to convert the unsafe code in the above mentioned question into safe code.
  4. 如何将上述问题中的不安全代码转换为安全代码。

1 个解决方案

#1


1  

Created and extension method that generates a HashCode for the provided string so that it is same even if the client and server are being executed in different environments (x86 or x64).

创建和扩展方法,为提供的字符串生成HashCode,即使客户端和服务器在不同的环境(x86或x64)中执行,它也是相同的。

#1


1  

Created and extension method that generates a HashCode for the provided string so that it is same even if the client and server are being executed in different environments (x86 or x64).

创建和扩展方法,为提供的字符串生成HashCode,即使客户端和服务器在不同的环境(x86或x64)中执行,它也是相同的。