Enum.TryParse 方法

定义

将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象。 用于指示转换是否成功的返回值。

重载

TryParse(Type, ReadOnlySpan<Char>, Object)

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

TryParse(Type, String, Object)

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

TryParse(Type, ReadOnlySpan<Char>, Boolean, Object)

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

TryParse(Type, String, Boolean, Object)

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

TryParse<TEnum>(ReadOnlySpan<Char>, TEnum)

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

TryParse<TEnum>(String, TEnum)

将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象。 用于指示转换是否成功的返回值。

TryParse<TEnum>(String, Boolean, TEnum)

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

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

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

TryParse(Type, ReadOnlySpan<Char>, Object)

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

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

public:
 static bool TryParse(Type ^ enumType, ReadOnlySpan<char> value, [Runtime::InteropServices::Out] System::Object ^ % result);
public static bool TryParse (Type enumType, ReadOnlySpan<char> value, out object? result);
static member TryParse : Type * ReadOnlySpan<char> * obj -> bool
Public Shared Function TryParse (enumType As Type, value As ReadOnlySpan(Of Char), ByRef result As Object) As Boolean

参数

enumType
Type

用于分析的枚举类型。

value
ReadOnlySpan<Char>

一个或多个枚举常量的名称或数值的范围表示形式。

result
Object

此方法返回 true时,包含一个表示已分析值的枚举常量。

返回

如果转换成功,则为 true;否则为 false

例外

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

适用于

TryParse(Type, String, Object)

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

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

public:
 static bool TryParse(Type ^ enumType, System::String ^ value, [Runtime::InteropServices::Out] System::Object ^ % result);
public static bool TryParse (Type enumType, string? value, out object? result);
public static bool TryParse (Type enumType, string value, out object result);
static member TryParse : Type * string * obj -> bool
Public Shared Function TryParse (enumType As Type, value As String, ByRef result As Object) As Boolean

参数

enumType
Type

用于分析的枚举类型。

value
String

一个或多个枚举常量的名称或数值的字符串表示形式。

result
Object

此方法返回 true时,包含一个表示已分析值的枚举常量。

返回

如果转换成功,则为 true;否则为 false

例外

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

适用于

TryParse(Type, ReadOnlySpan<Char>, Boolean, Object)

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

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

public:
 static bool TryParse(Type ^ enumType, ReadOnlySpan<char> value, bool ignoreCase, [Runtime::InteropServices::Out] System::Object ^ % result);
public static bool TryParse (Type enumType, ReadOnlySpan<char> value, bool ignoreCase, out object? result);
static member TryParse : Type * ReadOnlySpan<char> * bool * obj -> bool
Public Shared Function TryParse (enumType As Type, value As ReadOnlySpan(Of Char), ignoreCase As Boolean, ByRef result As Object) As Boolean

参数

enumType
Type

用于分析的枚举类型。

value
ReadOnlySpan<Char>

一个或多个枚举常量的名称或数值的范围表示形式。

ignoreCase
Boolean

若为 true,则在不区分大小写的模式下读取 enumType;若为 false,则在区分大小写的模式下读取 enumType

result
Object

此方法返回 true时,包含一个表示已分析值的枚举常量。

返回

如果转换成功,则为 true;否则为 false

例外

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

适用于

TryParse(Type, String, Boolean, Object)

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

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

public:
 static bool TryParse(Type ^ enumType, System::String ^ value, bool ignoreCase, [Runtime::InteropServices::Out] System::Object ^ % result);
public static bool TryParse (Type enumType, string? value, bool ignoreCase, out object? result);
public static bool TryParse (Type enumType, string value, bool ignoreCase, out object result);
static member TryParse : Type * string * bool * obj -> bool
Public Shared Function TryParse (enumType As Type, value As String, ignoreCase As Boolean, ByRef result As Object) As Boolean

参数

enumType
Type

用于分析的枚举类型。

value
String

一个或多个枚举常量的名称或数值的字符串表示形式。

ignoreCase
Boolean

若为 true,则在不区分大小写的模式下读取 value;若为 false,则在区分大小写的模式下读取 value

result
Object

此方法返回 true时,包含一个表示已分析值的枚举常量。

返回

如果转换成功,则为 true;否则为 false

例外

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

适用于

TryParse<TEnum>(ReadOnlySpan<Char>, TEnum)

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

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

public:
generic <typename TEnum>
 where TEnum : value class static bool TryParse(ReadOnlySpan<char> value, [Runtime::InteropServices::Out] TEnum % result);
public static bool TryParse<TEnum> (ReadOnlySpan<char> value, out TEnum result) where TEnum : struct;
static member TryParse : ReadOnlySpan<char> * 'Enum -> bool (requires 'Enum : struct)
Public Shared Function TryParse(Of TEnum As Structure) (value As ReadOnlySpan(Of Char), ByRef result As TEnum) As Boolean

类型参数

TEnum

result 对象的类型。

参数

value
ReadOnlySpan<Char>

一个或多个枚举常量的名称或数值的范围表示形式。

result
TEnum

此方法返回 true时,包含一个表示已分析值的枚举常量。

返回

如果转换成功,则为 true;否则为 false

例外

TEnum 不是枚举类型。

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

适用于

TryParse<TEnum>(String, TEnum)

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

将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象。 用于指示转换是否成功的返回值。

public:
generic <typename TEnum>
 where TEnum : value class static bool TryParse(System::String ^ value, [Runtime::InteropServices::Out] TEnum % result);
public static bool TryParse<TEnum> (string value, out TEnum result) where TEnum : struct;
public static bool TryParse<TEnum> (string? value, out TEnum result) where TEnum : struct;
static member TryParse : string * 'Enum -> bool (requires 'Enum : struct)
Public Shared Function TryParse(Of TEnum As Structure) (value As String, ByRef result As TEnum) As Boolean

类型参数

TEnum

要将 value 转换到的枚举类型。

参数

value
String

要转换的枚举名称或基础值的区分大小写字符串表示形式。

result
TEnum

此方法返回时,包含一个 类型的 TEnum 对象,如果分析操作成功,则其值由 value 表示。 如果分析操作失败,则包含 基础类型的 TEnum默认值。 此参数未经初始化即被传递。

返回

如果 true 参数成功转换,则为 value;否则为 false

例外

TEnum 不是枚举类型。

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

示例

以下示例定义枚举 Colors ,调用 TryParse<TEnum>(String, TEnum) 方法将字符串转换为相应的枚举值,并调用 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)
      {
         Colors colorValue;
         if (Enum.TryParse(colorString, out colorValue))
            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);
         else
            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
    match Enum.TryParse colorString with
    | true, colorValue ->
        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."
    | _ ->
        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
         Dim colorValue As Colors
         If [Enum].TryParse(colorString, colorValue) Then        
            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                    
         Else
            Console.WriteLine("{0} is not a member of the Colors enumeration.", colorString)
         End If
      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.

注解

TryParse<TEnum>(String, TEnum) 与 方法相同 Parse(Type, String) ,不同之处在于,如果转换失败,它返回 false 而不是引发异常。 分析枚举值的字符串表示形式时,无需进行异常处理。

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

如果 value 是与 的命名常量 TEnum不对应的名称,则该方法返回 false。 如果 value 是不表示枚举的基础值的整数的 TEnum 字符串表示形式,则 该方法返回其基础值 value 转换为整型类型的枚举成员。 如果此行为不可取,请调用 IsDefined 方法以确保整数的特定字符串表示形式实际上是 的成员 TEnum

如果分析操作失败,参数 result 包含默认值 0,该值可能不是基础 TEnum 枚举的成员。 如果未将任何值分配给 中的 TEnum命名常量,则默认值等于 的第一个成员 TEnum。 否则,默认值等于枚举中分配值为 0 的成员。

另请参阅

适用于

TryParse<TEnum>(String, Boolean, TEnum)

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

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

public:
generic <typename TEnum>
 where TEnum : value class static bool TryParse(System::String ^ value, bool ignoreCase, [Runtime::InteropServices::Out] TEnum % result);
public static bool TryParse<TEnum> (string value, bool ignoreCase, out TEnum result) where TEnum : struct;
public static bool TryParse<TEnum> (string? value, bool ignoreCase, out TEnum result) where TEnum : struct;
static member TryParse : string * bool * 'Enum -> bool (requires 'Enum : struct)
Public Shared Function TryParse(Of TEnum As Structure) (value As String, ignoreCase As Boolean, ByRef result As TEnum) As Boolean

类型参数

TEnum

要将 value 转换到的枚举类型。

参数

value
String

要转换的枚举名称或基础值的字符串表示形式。

ignoreCase
Boolean

若要不区分大小写,则为 true;若要区分大小写,则为 false

result
TEnum

此方法返回时,包含一个 类型的 TEnum 对象,如果分析操作成功,则其值由 value 表示。 如果分析操作失败,则包含 基础类型的 TEnum默认值。 此参数未经初始化即被传递。

返回

如果 true 参数成功转换,则为 value;否则为 false

例外

TEnum 不是枚举类型。

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

示例

以下示例定义枚举 Colors ,调用 TryParse<TEnum>(String, Boolean, TEnum) 方法将字符串转换为相应的枚举值,并调用 IsDefined 方法以确保特定整型值是枚举中 Colors 的基础值。 尝试 TryParse<TEnum>(String, Boolean, TEnum) 将命名常量的字符串表示形式转换为其等效枚举值时,方法使用不区分大小写的比较。

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)
      {
         Colors colorValue;
         if (Enum.TryParse(colorString, true, out colorValue))
            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);
         else
            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
    match Enum.TryParse(colorString, true) with
    | true, colorValue ->
        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."
    | _ ->
        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
         Dim colorValue As Colors
         If [Enum].TryParse(colorString, True, colorValue) Then        
            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                    
         Else
            Console.WriteLine("{0} is not a member of the Colors enumeration.", colorString)
         End If
      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.

注解

TryParse<TEnum>(String, Boolean, TEnum) 与 方法相同 Parse(Type, String, Boolean) ,不同之处在于,如果转换失败,它返回 false 而不是引发异常。 它无需在分析枚举值的字符串表示形式时进行异常处理。

参数 value 包含枚举成员的基础值或命名常量的字符串表示形式,或者由逗号 (,) 分隔的命名常量或基础值的列表。 如果 value 包含多个命名常量或值,则 中 value每个值、名称或逗号的前面或后面可以有一个或多个空格。 如果 value 是列表, result 则 反映指定名称的值或与按位 OR 运算组合的基础值。 如果 value 是枚举值名称的字符串表示形式,则 与 value 枚举名称的比较取决于 ignoreCase 参数。 如果 true为 ,则比较不区分大小写;如果 false为 ,则区分大小写。

如果 value 的名称与 的命名常量 TEnum不对应,则该方法返回 false。 如果 value 是不表示枚举的基础值的整数的 TEnum 字符串表示形式,则该方法返回其基础值 value 转换为整型类型的枚举成员。 如果不需要此行为,请调用 IsDefined 方法以确保整数的特定字符串表示形式实际上是 的成员 TEnum

如果分析操作失败,参数 result 将包含默认值 0,该默认值可能不是基础 TEnum 枚举的成员。 如果未向 中的 TEnum命名常量分配任何值,则默认值等于 的第一个成员 TEnum。 否则,默认值等于 枚举中分配值为 0 的成员。

另请参阅

适用于

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

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

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

public:
generic <typename TEnum>
 where TEnum : value class static bool TryParse(ReadOnlySpan<char> value, bool ignoreCase, [Runtime::InteropServices::Out] TEnum % result);
public static bool TryParse<TEnum> (ReadOnlySpan<char> value, bool ignoreCase, out TEnum result) where TEnum : struct;
static member TryParse : ReadOnlySpan<char> * bool * 'Enum -> bool (requires 'Enum : struct)
Public Shared Function TryParse(Of TEnum As Structure) (value As ReadOnlySpan(Of Char), ignoreCase As Boolean, ByRef result As TEnum) As Boolean

类型参数

TEnum

result 对象的类型。

参数

value
ReadOnlySpan<Char>

一个或多个枚举常量的名称或数值的范围表示形式。

ignoreCase
Boolean

若要不区分大小写,则为 true;若要区分大小写,则为 false

result
TEnum

此方法返回 true时,包含表示已分析值的枚举常量。

返回

如果转换成功,则为 true;否则为 false

例外

TEnum 不是枚举类型。

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

适用于