Enum.Parse 方法

定義

將一或多個列舉常數之名稱或數值的字串表示轉換為相等的列舉物件。

多載

名稱 Description
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>)

來源:
Enum.cs
來源:
Enum.cs
來源:
Enum.cs
來源:
Enum.cs
來源:
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)

來源:
Enum.cs
來源:
Enum.cs
來源:
Enum.cs
來源:
Enum.cs
來源:
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 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)

來源:
Enum.cs
來源:
Enum.cs
來源:
Enum.cs
來源:
Enum.cs
來源:
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)

來源:
Enum.cs
來源:
Enum.cs
來源:
Enum.cs
來源:
Enum.cs
來源:
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)

來源:
Enum.cs
來源:
Enum.cs
來源:
Enum.cs
來源:
Enum.cs
來源:
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)

來源:
Enum.cs
來源:
Enum.cs
來源:
Enum.cs
來源:
Enum.cs
來源:
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>)

來源:
Enum.cs
來源:
Enum.cs
來源:
Enum.cs
來源:
Enum.cs
來源:
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)

來源:
Enum.cs
來源:
Enum.cs
來源:
Enum.cs
來源:
Enum.cs
來源:
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 是一種布林背列舉類型。

適用於