Enum.Parse 方法

定义

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

重载

Parse(Type, ReadOnlySpan<Char>)

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

Parse(Type, String)

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

Parse(Type, ReadOnlySpan<Char>, Boolean)

将一个或多个枚举常量的名称或数值的字符表示形式转换为等效的枚举对象。 一个参数指定该操作是否不区分大小写。

Parse(Type, String, Boolean)

将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象。 一个参数指定该操作是否不区分大小写。

Parse<TEnum>(String, Boolean)

TEnum 指定的一个或多个枚举常数的名称或数字值的字符串表示形式转换成等效的枚举对象。 一个参数指定该操作是否不区分大小写。

Parse<TEnum>(ReadOnlySpan<Char>, Boolean)

将指定的 TEnum 一个或多个枚举常量的名称或数值的字符表示形式转换为等效的枚举对象。 一个参数指定该操作是否不区分大小写。

Parse<TEnum>(ReadOnlySpan<Char>)

将指定的 TEnum 一个或多个枚举常量的名称或数值的字符表示形式转换为等效的枚举对象。

Parse<TEnum>(String)

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

Parse(Type, ReadOnlySpan<Char>)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

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

public:
 static System::Object ^ Parse(Type ^ enumType, ReadOnlySpan<char> value);
public static object Parse (Type enumType, ReadOnlySpan<char> value);
static member Parse : Type * ReadOnlySpan<char> -> obj
Public Shared Function Parse (enumType As Type, value As ReadOnlySpan(Of Char)) As Object

参数

enumType
Type

枚举类型。

value
ReadOnlySpan<Char>

一个范围,其中包含要转换的名称或值。

返回

enumType 类型的对象,其值由 value 表示。

例外

enumTypenull

enumType 不是 Enum

value 为空字符串或只包含空格。

value 是一个名称,而不是为该枚举定义的命名常量之一。

value 超出了 enumType 的基础类型范围。

.NET 8 及更高版本: enumType 是一种布尔支持的枚举类型。

适用于

Parse(Type, String)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

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

public:
 static System::Object ^ Parse(Type ^ enumType, System::String ^ value);
public static object Parse (Type enumType, string value);
[System.Runtime.InteropServices.ComVisible(true)]
public static object Parse (Type enumType, string value);
static member Parse : Type * string -> obj
[<System.Runtime.InteropServices.ComVisible(true)>]
static member Parse : Type * string -> obj
Public Shared Function Parse (enumType As Type, value As String) As Object

参数

enumType
Type

枚举类型。

value
String

包含要转换的值或名称的字符串。

返回

enumType 类型的对象,其值由 value 表示。

属性

例外

enumTypevaluenull

enumType 不是 Enum

- 或 -

value 为空字符串或只包含空格。

- 或 -

value 是一个名称,而不是为该枚举定义的命名常量之一。

value 超出了 enumType 的基础类型范围。

.NET 8 及更高版本: enumType 是一种布尔支持的枚举类型。

示例

以下示例使用 Parse(Type, String) 方法分析通过调用 GetNames 方法创建的字符串数组。 它还使用 Parse(Type, String) 方法分析由位字段组成的枚举值。

using namespace System;

[Flags]
enum class Colors
{
   Red = 1,
   Green = 2,
   Blue = 4,
   Yellow = 8
};

int main()
{
   Console::WriteLine(  "The entries of the Colors enumeration are:" );
   Array^ a = Enum::GetNames( Colors::typeid );
   Int32 i = 0;
   while ( i < a->Length )
   {
      Object^ o = a->GetValue( i );
      Console::WriteLine( o->ToString() );
      i++;
   }

   Console::WriteLine();
   Object^ orange = Enum::Parse( Colors::typeid,  "Red, Yellow" );
   Console::WriteLine("The orange value has the combined entries of {0}", orange );
}

/*
This code example produces the following results:

The entries of the Colors Enum are:
Red
Green
Blue
Yellow

The orange value has the combined entries of Red, Yellow

*/
using System;

public class ParseTest
{
    [Flags]
    enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 };

    public static void Main()
    {
        Console.WriteLine("The entries of the Colors enumeration are:");
        foreach (string colorName in Enum.GetNames(typeof(Colors)))
        {
            Console.WriteLine("{0} = {1:D}", colorName,
                                         Enum.Parse(typeof(Colors), colorName));
        }
        Console.WriteLine();

        Colors orange = (Colors) Enum.Parse(typeof(Colors), "Red, Yellow");
        Console.WriteLine("The orange value {0:D} has the combined entries of {0}",
                           orange);
    }
}

/*
This code example produces the following results:

The entries of the Colors Enum are:
Red = 1
Green = 2
Blue = 4
Yellow = 8

The orange value 9 has the combined entries of Red, Yellow

*/
open System

[<Flags>]
type Colors =
    | Red = 1
    | Green = 2
    | Blue = 4
    | Yellow = 8

printfn "The entries of the Colors enumeration are:"
for colorName in Enum.GetNames typeof<Colors> do
    printfn $"{colorName} = {Enum.Parse(typeof<Colors>, colorName):D}"
printfn ""

let orange = Enum.Parse(typeof<Colors>, "Red, Yellow") :?> Colors
printfn $"The orange value {orange:D} has the combined entries of {orange}"

// This code example produces the following results:
//     The entries of the Colors Enum are:
//     Red = 1
//     Green = 2
//     Blue = 4
//     Yellow = 8
//    
//     The orange value 9 has the combined entries of Red, Yellow
Public Class ParseTest

    <Flags()> _
    Enum Colors
        Red = 1
        Green = 2
        Blue = 4
        Yellow = 8
    End Enum

    Public Shared Sub Main()
        Console.WriteLine("The entries of the Colors enumeration are:")
        Dim colorName As String
        For Each colorName In [Enum].GetNames(GetType(Colors))
            Console.WriteLine("{0} = {1:D}", colorName, [Enum].Parse(GetType(Colors), colorName))
        Next
        Console.WriteLine()

        Dim orange As Colors = CType([Enum].Parse(GetType(Colors), "Red, Yellow"), Colors)
        Console.WriteLine("The orange value {0:D} has the combined entries of {0}", orange)
    End Sub
End Class

'This example displays the following output:
'
'The entries of the Colors Enum are:
'Red = 1
'Green = 2
'Blue = 4
'Yellow = 8
'
'The myOrange value 9 has the combined entries of Red, Yellow
'

注解

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

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

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.
open System

[<Flags>]
type Colors =
    | None = 0
    | Red = 1
    | Green = 2
    | Blue = 4

let colorStrings = [ "0"; "2"; "8"; "blue"; "Blue"; "Yellow"; "Red, Green" ]
for colorString in colorStrings do
    try
        let colorValue = Enum.Parse(typeof<Colors>, colorString) :?> Colors
        if Enum.IsDefined(typeof<Colors>, colorValue) || (string colorValue).Contains "," then
            printfn $"Converted '{colorString}' to {colorValue}."
        else
            printfn $"{colorString} is not an underlying value of the Colors enumeration."
    with :? ArgumentException ->
        printfn $"'{colorString}' is not a member of the Colors enumeration."
// 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.
<Flags> Enum Colors As Integer
   None = 0
   Red = 1
   Green = 2
   Blue = 4
End Enum

Module Example
   Public Sub Main()
      Dim colorStrings() As String = {"0", "2", "8", "blue", "Blue", "Yellow", "Red, Green"}
      For Each colorString As String In colorStrings
         Try
            Dim colorValue As Colors = CType([Enum].Parse(GetType(Colors), colorString), Colors)        
            If [Enum].IsDefined(GetType(Colors), colorValue) Or colorValue.ToString().Contains(",") Then 
               Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString())
            Else
               Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString)            
            End If                    
         Catch e As ArgumentException
            Console.WriteLine("'{0}' is not a member of the Colors enumeration.", colorString)
         End Try
      Next
   End Sub
End Module
' 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.

此操作区分大小写。

另请参阅

适用于

Parse(Type, ReadOnlySpan<Char>, Boolean)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

将一个或多个枚举常量的名称或数值的字符表示形式转换为等效的枚举对象。 一个参数指定该操作是否不区分大小写。

public:
 static System::Object ^ Parse(Type ^ enumType, ReadOnlySpan<char> value, bool ignoreCase);
public static object Parse (Type enumType, ReadOnlySpan<char> value, bool ignoreCase);
static member Parse : Type * ReadOnlySpan<char> * bool -> obj
Public Shared Function Parse (enumType As Type, value As ReadOnlySpan(Of Char), ignoreCase As Boolean) As Object

参数

enumType
Type

枚举类型。

value
ReadOnlySpan<Char>

一个范围,其中包含要转换的名称或值。

ignoreCase
Boolean

true 为忽略大小写;false 为考虑大小写。

返回

enumType 类型的对象,其值由 value 表示。

例外

enumTypenull

enumType 不是 Enum

value 为空字符串或只包含空格。

value 是一个名称,而不是为该枚举定义的命名常量之一。

value 超出了 的基础类型的范围 enumType

.NET 8 及更高版本: enumType 是一种布尔支持的枚举类型。

适用于

Parse(Type, String, Boolean)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象。 一个参数指定该操作是否不区分大小写。

public:
 static System::Object ^ Parse(Type ^ enumType, System::String ^ value, bool ignoreCase);
public static object Parse (Type enumType, string value, bool ignoreCase);
[System.Runtime.InteropServices.ComVisible(true)]
public static object Parse (Type enumType, string value, bool ignoreCase);
static member Parse : Type * string * bool -> obj
[<System.Runtime.InteropServices.ComVisible(true)>]
static member Parse : Type * string * bool -> obj
Public Shared Function Parse (enumType As Type, value As String, ignoreCase As Boolean) As Object

参数

enumType
Type

枚举类型。

value
String

包含要转换的值或名称的字符串。

ignoreCase
Boolean

true 为忽略大小写;false 为考虑大小写。

返回

enumType 类型的对象,其值由 value 表示。

属性

例外

enumTypevaluenull

enumType 不是 Enum

- 或 -

value 为空字符串 ("") 或只包含空格。

- 或 -

value 是一个名称,而不是为该枚举定义的命名常量之一。

value 超出了 enumType 的基础类型范围。

.NET 8 及更高版本: enumType 是一种布尔支持的枚举类型。

示例

以下示例使用 Parse(Type, String, Boolean) 方法分析通过调用 GetNames 方法创建的字符串数组。 它还使用 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, true);
            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.
//       Converted 'blue' to Blue.
//       Converted 'Blue' to Blue.
//       Yellow is not a member of the Colors enumeration.
//       Converted 'Red, Green' to Red, Green.
open System

[<Flags>]
type Colors =
    | None = 0
    | Red = 1
    | Green = 2
    | Blue = 4

let colorStrings = [ "0"; "2"; "8"; "blue"; "Blue"; "Yellow"; "Red, Green" ]
for colorString in colorStrings do
    try
        let colorValue = Enum.Parse(typeof<Colors>, colorString, true) :?> Colors
        if Enum.IsDefined(typeof<Colors>, colorValue) || (string colorValue).Contains "," then
            printfn $"Converted '{colorString}' to {colorValue}."
        else
            printfn $"{colorString} is not an underlying value of the Colors enumeration."
    with :? ArgumentException ->
        printfn $"{colorString} is not a member of the Colors enumeration."

// The example displays the following output:
//       Converted '0' to None.
//       Converted '2' to Green.
//       8 is not an underlying value of the Colors enumeration.
//       Converted 'blue' to Blue.
//       Converted 'Blue' to Blue.
//       Yellow is not a member of the Colors enumeration.
//       Converted 'Red, Green' to Red, Green.
<Flags> Enum Colors As Integer
   None = 0
   Red = 1
   Green = 2
   Blue = 4
End Enum

Module Example
   Public Sub Main()
      Dim colorStrings() As String = {"0", "2", "8", "blue", "Blue", "Yellow", "Red, Green"}
      For Each colorString As String In colorStrings
         Try
            Dim colorValue As Colors = CType([Enum].Parse(GetType(Colors), colorString, True), Colors)        
            If [Enum].IsDefined(GetType(Colors), colorValue) Or colorValue.ToString().Contains(",") Then 
               Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString())
            Else
               Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString)            
            End If                    
         Catch e As ArgumentException
            Console.WriteLine("{0} is not a member of the Colors enumeration.", colorString)
         End Try
      Next
   End Sub
End Module
' The example displays the following output:
'       Converted '0' to None.
'       Converted '2' to Green.
'       8 is not an underlying value of the Colors enumeration.
'       Converted 'blue' to Blue.
'       Converted 'Blue' to Blue.
'       Yellow is not a member of the Colors enumeration.
'       Converted 'Red, Green' to Red, Green.

注解

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

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

参数 ignoreCase 指定此操作是否区分大小写。

另请参阅

适用于

Parse<TEnum>(String, Boolean)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

TEnum 指定的一个或多个枚举常数的名称或数字值的字符串表示形式转换成等效的枚举对象。 一个参数指定该操作是否不区分大小写。

public:
generic <typename TEnum>
 where TEnum : value class static TEnum Parse(System::String ^ value, bool ignoreCase);
public static TEnum Parse<TEnum> (string value, bool ignoreCase) where TEnum : struct;
static member Parse : string * bool -> 'Enum (requires 'Enum : struct)
Public Shared Function Parse(Of TEnum As Structure) (value As String, ignoreCase As Boolean) As TEnum

类型参数

TEnum

枚举类型。

参数

value
String

包含要转换的值或名称的字符串。

ignoreCase
Boolean

true 为忽略大小写;false 为考虑大小写。

返回

TEnum

TEnum 类型的对象,其值由 value 表示。

例外

TEnum 不是 Enum 类型。

valuenull

value 不包含枚举信息。

.NET 8 及更高版本: TEnum 是一种布尔支持的枚举类型。

适用于

Parse<TEnum>(ReadOnlySpan<Char>, Boolean)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

将指定的 TEnum 一个或多个枚举常量的名称或数值的字符表示形式转换为等效的枚举对象。 一个参数指定该操作是否不区分大小写。

public:
generic <typename TEnum>
 where TEnum : value class static TEnum Parse(ReadOnlySpan<char> value, bool ignoreCase);
public static TEnum Parse<TEnum> (ReadOnlySpan<char> value, bool ignoreCase) where TEnum : struct;
static member Parse : ReadOnlySpan<char> * bool -> 'Enum (requires 'Enum : struct)
Public Shared Function Parse(Of TEnum As Structure) (value As ReadOnlySpan(Of Char), ignoreCase As Boolean) As TEnum

类型参数

TEnum

枚举类型。

参数

value
ReadOnlySpan<Char>

一个范围,其中包含要转换的名称或值。

ignoreCase
Boolean

true 为忽略大小写;false 为考虑大小写。

返回

TEnum

TEnum 类型的 对象 TEnum ,其值由 value表示。

例外

TEnum 不是 Enum 类型。

value 不包含枚举信息。

.NET 8 及更高版本: TEnum 是一种布尔支持的枚举类型。

适用于

Parse<TEnum>(ReadOnlySpan<Char>)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

将指定的 TEnum 一个或多个枚举常量的名称或数值的字符表示形式转换为等效的枚举对象。

public:
generic <typename TEnum>
 where TEnum : value class static TEnum Parse(ReadOnlySpan<char> value);
public static TEnum Parse<TEnum> (ReadOnlySpan<char> value) where TEnum : struct;
static member Parse : ReadOnlySpan<char> -> 'Enum (requires 'Enum : struct)
Public Shared Function Parse(Of TEnum As Structure) (value As ReadOnlySpan(Of Char)) As TEnum

类型参数

TEnum

枚举类型。

参数

value
ReadOnlySpan<Char>

一个范围,其中包含要转换的名称或值。

返回

TEnum

TEnum 类型的 对象 TEnum ,其值由 value表示。

例外

TEnum 不是 Enum 类型。

value 不包含枚举信息。

.NET 8 及更高版本: TEnum 是一种布尔支持的枚举类型。

适用于

Parse<TEnum>(String)

Source:
Enum.cs
Source:
Enum.cs
Source:
Enum.cs

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

public:
generic <typename TEnum>
 where TEnum : value class static TEnum Parse(System::String ^ value);
public static TEnum Parse<TEnum> (string value) where TEnum : struct;
static member Parse : string -> 'Enum (requires 'Enum : struct)
Public Shared Function Parse(Of TEnum As Structure) (value As String) As TEnum

类型参数

TEnum

枚举类型。

参数

value
String

包含要转换的值或名称的字符串。

返回

TEnum

TEnum 类型的对象,其值由 value 表示。

例外

TEnum 不是 Enum 类型。

valuenull

value 不包含枚举信息。

.NET 8 及更高版本: TEnum 是一种布尔支持的枚举类型。

适用于