使用hashcode为枚举生成值 [英] Using hashcode to generate value for enum

查看:186
本文介绍了使用hashcode为枚举生成值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道,所有的枚举都被编译为常量,这意味着你可以获取意外结果如果您使用来自不同程序集的枚举。



显式设置顺序枚举数字不是帮助,因为如果要为新值留出空间,则必须执行基本的linenumber样式间距是naff。



所以我想知道使用枚举值名称的哈希码作为字符串,这是很容易生成一个脚本。



这是个好主意吗?我有问题,哈希码可以返回的负值吗?



编辑
对于那些问为什么,我引用链接的角色案例:



给出一个程序集:

 枚举MyEnum 
{
红色,
蓝色,
}

如果您在另一个程序集中使用MyEnum.Red.ToString(),并且之间有人已将您的枚举重新编译为:

 枚举MyEnum 
{
黑色,
红色,
蓝色,
}

在运行时,你会得到黑色。 (注意,我对这里的ToString并不特别感兴趣,问题是如果调用代码要求MyEnum.Red,它实际上是引用MyEnum.Black )



另一个例子来说明显式顺序编号的问题:

 code>枚举ECountry 
{
阿富汗= 1,
巴巴多斯= 2,
智利= 3,
...
赞比亚= 243 ,
}

如果 Giggleswick 突然宣布自己是共和国,

解决方案

不,这不是一个好主意。



你不能从它的哈希码重新创建一个字符串。您可以循环访问名称并将其哈希码与您所拥有的哈希码进行比较,但由于哈希代码冲突,仍然存在一个错误的冒险风险。风险相当小,但仍然不是可靠的使用方法。



只需使用枚举值的名称,如果您需要从一个版本的枚举转换到另一个 ToString 方法为您提供名称,您可以使用 Enum.Parse 方法获取枚举值字符串。



编辑:

使用字符串值当然要求您在每个程序集中都有一次枚举,以便每次编译。如果您在引用的程序集中使用枚举,那么您必须给每个值一个特定的数字表示,否则您根本没有任何可以保持值从一个版本到下一个版本。


As we know, all enums are compiled as constants, which means you can get unexpected results if you use an enum from a different assembly.

Settings sequential enum numbers explicitly is no help, because if you want to leave room for new values, you have to do basic-linenumber-style spacing, which is naff.

So I wondered about using the hashcode of the Enum value name as string, which is fairly easy to generate a script for.

Is this a good idea? Will I have problems with the negative values that hashcode can return?

EDIT For those asking why, I quote the linked "corner case":

Given an assembly with:

enum MyEnum
{
    Red,
    Blue,
}

if you use MyEnum.Red.ToString() in another assembly, and in between times someone has recompiled your enum to:

enum MyEnum
{
    Black,
    Red,
    Blue,
}

at runtime, you will get "Black".

(NB, I am not especially interested in the ToString here, the problem is that if calling code asks for MyEnum.Red, it's actually referencing MyEnum.Black)

Another example to illustrate the problem with explicit sequential numbering:

enum ECountry
{
    Afghanistan = 1,
    Barbados = 2,
    Chile = 3,
    ...
    Zambia = 243, 
}

If Giggleswick suddenly declares itself a republic, what do you do?

解决方案

No, it's not a good idea.

You can't safely recreate a string from it's hash code. You can loop through the names and compare their hash code with the one that you have, but there is still a small risk of a false positive due to hash code collisions. The risk is rather small, but it's still not a reliable method to use.

Just use the name of the enum value if you need to convert from one version of an enum to another. The ToString method gives you the name, and you can use the Enum.Parse method to get the enum value from the string.

Edit:
Using the string value of course requires that you have one occurance of the enum in each assembly so that it's compiled each time. If you are using an enum in a referenced assembly, then you have to give each value a specific numeric representation, otherwise you don't have anything at all that keeps the value consistent from one version to the next.

这篇关于使用hashcode为枚举生成值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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