使用 Enum.Parse() 时出现意外结果
class Program
{
static void Main(string[] args)
{
String value = "Two";
Type enumType = typeof(Numbers);
Numbers number = (Numbers)Enum.Parse(enumType, value);
Console.WriteLine(Enum.Parse(enumType, value));
}
public enum Numbers : int
{
One,
Two,
Three,
Four,
FirstValue = 1
}
}
这是我在应用程序中使用的枚举的简化版本。 某些枚举名称没有值的原因是因为我使用它们的名称作为参数进行 Enum.Parse 操作,而具有值的枚举名称是从 int 解析的。
如果您单步执行上面的代码并调查“number”变量,您会发现它实际上是“Two”,但控制台中的输出是“FirstValue”。 现在我不明白为什么,你呢?
好吧,解决方案很简单——只需为无价值的枚举赋予一个值即可。 但我还是很好奇。
class Program
{
static void Main(string[] args)
{
String value = "Two";
Type enumType = typeof(Numbers);
Numbers number = (Numbers)Enum.Parse(enumType, value);
Console.WriteLine(Enum.Parse(enumType, value));
}
public enum Numbers : int
{
One,
Two,
Three,
Four,
FirstValue = 1
}
}
This is a simplified version of an enum I use in an application. The reason to why some of the enum names doesn't have a value is because I do Enum.Parse with their names as argument, while the ones with a value is parsed from an int.
If you would step through the code above and investigate the 'number' variable, you would see that it in fact is 'Two', but the output in console is 'FirstValue'. At this point I can't see why, do you?
Okay, the solution is simple - just give the valueless enums a value. But I'm still curious.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑
FirstValue
和Two
的内部值都是 1,因此系统不知道要输出哪个字符串。每个枚举值都有一个唯一的整数值,但并非每个整数值都有唯一的枚举值。
当您解析“two”时,它会在内部存储为整数
1
。 然后,当您尝试将其转换回字符串时,根据用于查找该名称的技术,您可能会得到"Two"
或"FirstValue"
。 正如您所说,解决方案是为每个枚举值指定一个定义的整数值。I suspect that both
FirstValue
andTwo
have an internal value of 1, so the system doesn't know which string to output.There is a unique integer value for every enum value, but there is not a unique enum value for every integer value.
When you parse
"two"
, it gets stored internally as the integer1
. Then when you try and convert it back to a string, depending on the technique used to lookup that name, you could get either"Two"
or"FirstValue"
. As you stated, the solution is to give every enum value a defined integer value.这是您的问题的一个有趣的转折,请尝试以下枚举...
console.WriteLine(...) 现在将打印“Two”!
Two
和FirstValue
都表示相同的数字1
,但看到的实际值取决于数字如何转换为其字符串表示形式,反之亦然。Enum 类使用反射来获取数字的名称,然后将它们存储在数组中,但在执行此操作之前它会对整个事物进行排序。 然后 Enum.ToString() 对排序后的值进行二分搜索以获得字符串表示形式。 由于这样做的方式,您可能会得到不同的结果,具体取决于枚举中元素的数量!
现在,对于 VS 中“看到”的值,我怀疑枚举的调试器可视化工具使用了自己的算法来纠正(?)这个错误。
Here is an interesting twist to your problem, try the following Enum...
The console.WriteLine(...) will now print "Two"!
Both
Two
andFirstValue
represent the same number1
but the actual value seen depends on how the number was converted to its string representation and vice-versa.The Enum class uses reflection to get the names of the numbers and then stores them in arrays but it sorts the the whole thing before it does so. Then Enum.ToString() does a binary search on the sorted values to get the string representation. Due to the way this is done you may get a different result depending on the number of elements you have in the enumeration!
Now as for the value "seen" in VS I suspect the debugger visualizer for enums uses an algorithm of its own which corrects(?) this bug.