混淆解析一个枚举 [英] Confusion with parsing an Enum
问题描述
我正在将一个字符串类型的数值转换成相应的枚举。当我测试我的代码时,我发现有趣的行为让我感到困惑。
I am converting a numeric value that is of string type into a corresponding Enum. While I was testing my code, I discovered interesting behavior that has me confused.
使用下面的代码示例,有人可以明白为什么如果/当s变量的值不匹配一个值时,不会抛出异常的枚举值?另外,如何将sEnum var设置为在Stooge枚举的定义中不存在的值?
Using the code example below, can somebody shed light on why an exception isn't thrown if/when the "s" variable has a value that doesn't match one of the Enum values? Also, how is that the sEnum var can be set to a value that doesn't exist within the definition of the Stooge enum?
class Program
{
enum Stooge
{
Unspecified,
Moe,
Larry,
Curly,
Shemp
}
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("Enter a number...");
string s = Console.ReadLine();
Stooge sEnum = (Stooge)(int.Parse(s)); //Why doesn't this line throw if s != 0, 1, 2, 3, or 4?
Console.WriteLine("\r\nYou entered: {0}\r\nEnum String Value: {1}\r\nEnum Int Value: {2}\r\n", s, sEnum.ToString(), (int)sEnum);
}
}
}
推荐答案
这是创建.NET的人的一个决定。枚举由另一个值类型( int
, short
, byte
等),因此它实际上可以具有对这些值类型有效的任何值。
This was a decision on the part of the people who created .NET. An enum is backed by another value type (int
, short
, byte
, etc), and so it can actually have any value that is valid for those value types.
我个人不是这个工作方式的粉丝,所以我做了一系列的实用方法:
I personally am not a fan of the way this works, so I made a series of utility methods:
/// <summary>
/// Utility methods for enum values. This static type will fail to initialize
/// (throwing a <see cref="TypeInitializationException"/>) if
/// you try to provide a value that is not an enum.
/// </summary>
/// <typeparam name="T">An enum type. </typeparam>
public static class EnumUtil<T>
where T : struct, IConvertible // Try to get as much of a static check as we can.
{
// The .NET framework doesn't provide a compile-checked
// way to ensure that a type is an enum, so we have to check when the type
// is statically invoked.
static EnumUtil()
{
// Throw Exception on static initialization if the given type isn't an enum.
Require.That(typeof (T).IsEnum, () => typeof(T).FullName + " is not an enum type.");
}
/// <summary>
/// In the .NET Framework, objects can be cast to enum values which are not
/// defined for their type. This method provides a simple fail-fast check
/// that the enum value is defined, and creates a cast at the same time.
/// Cast the given value as the given enum type.
/// Throw an exception if the value is not defined for the given enum type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="enumValue"></param>
/// <exception cref="InvalidCastException">
/// If the given value is not a defined value of the enum type.
/// </exception>
/// <returns></returns>
public static T DefinedCast(object enumValue)
{
if (!System.Enum.IsDefined(typeof(T), enumValue))
throw new InvalidCastException(enumValue + " is not a defined value for enum type " +
typeof (T).FullName);
return (T) enumValue;
}
/// <summary>
///
/// </summary>
/// <param name="enumValue"></param>
/// <returns></returns>
public static T Parse(string enumValue)
{
var parsedValue = (T)System.Enum.Parse(typeof (T), enumValue);
//Require that the parsed value is defined
Require.That(parsedValue.IsDefined(),
() => new ArgumentException(string.Format("{0} is not a defined value for enum type {1}",
enumValue, typeof(T).FullName)));
return parsedValue;
}
public static bool IsDefined(T enumValue)
{
return System.Enum.IsDefined(typeof (T), enumValue);
}
}
这样我可以说:
if(!sEnum.IsDefined()) throw new Exception(...);
...或:
EnumUtil<Stooge>.Parse(s); // throws an exception if s is not a defined value.
这篇关于混淆解析一个枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!