为什么Enum.Parse创建不确定项? [英] Why does Enum.Parse create undefined entries?

查看:141
本文介绍了为什么Enum.Parse创建不确定项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Program
{
    static void Main(string[] args)
    {
        string value = "12345";
        Type enumType = typeof(Fruits);
        Fruits fruit = Fruits.Apple;
        try
        {
            fruit = (Fruits) Enum.Parse(enumType, value);
        }
        catch (ArgumentException)
        {
            Console.WriteLine(String.Format("{0} is no healthy food.", value));
        }
        Console.WriteLine(String.Format("You should eat at least one {0} per day.", fruit));
        Console.ReadKey();
    }

    public enum Fruits
    {
        Apple,
        Banana,
        Orange
    }
}

如果你执行上面的结果显示了code:

If you execute the code above the result shows:

您应该每天吃至少一个12345。

You should eat at least one 12345 per day.

我真的很期待,如果一个未知的名称(字符串)传递给抛出一个ArgumentException。以一仔细看Enum.Parse定义揭示了:

I really expected an ArgumentException to be thrown if a unknown name (string) is passed. Taking a close look at the Enum.Parse definition reveals:

摘要:
  该字符串重新名称presentation或数值的一个或多个枚举常量为等效的枚举对象转换。

Summary:
Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.

例外:
  的ArgumentException 的:enumType不是一个枚举。 - 或 - 值是空字符串或者只包含空格。 - 或 - 值是出了名的,但不为枚举中定义的命名常量之一。

Exceptions:
ArgumentException: enumType is not an Enum. -or- value is either an empty string or only contains white space. -or- value is a name, but not one of the named constants defined for the enumeration.

即。如果一个字符串再一个整数的presentation传递,一个新的枚举值造成的,现在异常是由设计抛出。这是否有意义?

I.e. if a string representation of an integer is passed, a new enum value is created and now exception is thrown by design. Does this make sense?

至少我现在知道叫 Enum.IsDefined(enumType,值)之前 Enum.Parse()

At least I now know to call Enum.IsDefined(enumType, value) prior to Enum.Parse()

推荐答案

在命名常量是文本重新presentation枚举的价值,而不是你已经分配给它的数量。

The "named constant" is the textual representation of an Enum's value, not the number that you've assigned to it.

如果您改变:

string value = "12345";

要:

string value = "Cake";

您会看到你期待的错误,因为值是一个名称,但对于枚举中定义的命名常量的一个也没有。。在这种情况下你传递的值的的名字,蛋糕,却没有一个枚举。

You'll see the error you're expecting, because "value is a name, but not one of the named constants defined for the enumeration.". In this instance the value you're passing in is a name, "Cake", but not one in the enumeration.

想想 Enum.Parse(enumType,值); 执行以下操作:

  1. 如果是一个空引用,抛出一个ArgumentNullException
  2. 一个在枚举命名常量的 enumType 的值。如果是的话,返回值从枚举并停止。
  3. 直接转换为基本类型的值(在这种情况下的Int32),如果是的话,返回值和停止(即使没有命名常量的值)。
  4. 值值直接转换为基本类型,但外面的基础类型的范围?例如的值是包含一个头号大于MAXINT字符串。如果是的话,抛出一个发生OverflowException
  5. 的值没有浇注料为基础类型?如果是的话,引发ArgumentException。
  1. If value is a null reference, throw an ArgumentNullException
  2. Is the value in value one of the named constants in the enumeration in enumType. If yes, return that value from the enumeration and stop.
  3. Is the value in value directly convertible to the underlying type (in this instance Int32), if yes, return that value and stop (even if there's no named constant for that value).
  4. Is the value in value directly convertible to the underlying type, but outside of the range of the underlying type? e.g. the value is a string containing a number one greater than MAXINT. If yes, throw an OverflowException.
  5. Is the value not castable to the underlying type? If yes, throw an ArgumentException.

这篇关于为什么Enum.Parse创建不确定项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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