System.Enum.Parse 方法 (Type, String)

方法描述

将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象。

语法定义(C# System.Enum.Parse 方法 (Type, String) 的用法)

[ComVisibleAttribute(true)]
public static Object Parse(
	Type enumType,
	string value
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
enumType System-Type 枚举类型。
value System-String 包含要转换的值或名称的字符串。
返回值 System.Object enumType 类型的对象,其值由 value 表示。

提示和注释

value 参数中包含枚举成员基础值字符串表示形式、已命名的常数或由逗号 (,) 分隔的已命名常数列表。 在 value 中的每个值、名称或逗号的前面或后面都可以有一个或多个空格。 如果 value 是一个列表,则返回值是各指定名称通过按位 OR 运算结合而成的值。

如果 value 是一个不与 enumType 的命名常数对应的名称,则此方法将引发 ArgumentException。 如果 value 是一个不表示 enumType 枚举基础值的整数的字符串表示形式,则此方法将返回基础值是已转换为整型的 value 的枚举成员。 如果不希望此行为发生,则请调用 IsDefined 方法以确保整数的特定字符串表示形式实际是 enumType 的成员。 下面的示例定义 Colors 枚举,调用 Parse(Type, String) 方法将字符串转换为其对应的枚举值,并调用 IsDefined 方法以确保特定整数值是 Colors 枚举中的基础值。

C#

VB

复制

using System;

[Flags] enum Colors { None=0, Red = 1, Green = 2, Blue = 4 };

public class Example

{

public static void Main()

{

string[] colorStrings = { "0", "2", "8", "blue", "Blue", "Yellow", "Red, Green" };

foreach (string colorString in colorStrings)

{

try {

Colors colorValue = (Colors) Enum.Parse(typeof(Colors), colorString);

if (Enum.IsDefined(typeof(Colors), colorValue) | colorValue.ToString().Contains(","))

Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString());

else

Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString);

}

catch (ArgumentException) {

Console.WriteLine("'{0}' is not a member of the Colors enumeration.", colorString);

}

}

}

}

// The example displays the following output:

// Converted '0' to None.

// Converted '2' to Green.

// 8 is not an underlying value of the Colors enumeration.

// 'blue' is not a member of the Colors enumeration.

// Converted 'Blue' to Blue.

// 'Yellow' is not a member of the Colors enumeration.

// Converted 'Red, Green' to Red, Green.

此运算区分大小写。

System.Enum.Parse 方法 (Type, String)例子

它还使用 Parse(Type, String) 方法来分析组成位字段的枚举值。

using System;

[Flags] enum Colors { None=0, Red = 1, Green = 2, Blue = 4 };

public class Example
{
   public static void Main()
   {
      string[] colorStrings = { "0", "2", "8", "blue", "Blue", "Yellow", "Red, Green" };
      foreach (string colorString in colorStrings)
      {
         try {
            Colors colorValue = (Colors) Enum.Parse(typeof(Colors), colorString);        
            if (Enum.IsDefined(typeof(Colors), colorValue) | colorValue.ToString().Contains(","))  
               Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString());
            else
               Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString);
         }
         catch (ArgumentException) {
            Console.WriteLine("'{0}' is not a member of the Colors enumeration.", colorString);
         }
      }
   }
}
// The example displays the following output:
//       Converted '0' to None.
//       Converted '2' to Green.
//       8 is not an underlying value of the Colors enumeration.
//       'blue' is not a member of the Colors enumeration.
//       Converted 'Blue' to Blue.
//       'Yellow' is not a member of the Colors enumeration.
//       Converted 'Red, Green' to Red, Green.

异常

异常 异常描述
ArgumentNullException enumType 或 value 为 null。
ArgumentException
  • enumType 不是 Enum。
  • value 为空字符串或只包含空白。
  • value 是一个名称,但不是为该枚举定义的命名常量之一。
OverflowException value 超出 enumType 基础类型的范围。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。