Enum.TryParse 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将一个或多个枚举常量的名称或数值的名称或数值的字符串表示形式转换为等效的枚举对象。 返回值指示转换是否成功。
重载
| 名称 | 说明 |
|---|---|
| TryParse(Type, String, Boolean, Object) |
将一个或多个枚举常量的名称或数值的名称或数值的字符串表示形式转换为等效的枚举对象。 |
| TryParse(Type, String, Object) |
将一个或多个枚举常量的名称或数值的名称或数值的字符串表示形式转换为等效的枚举对象。 |
| TryParse<TEnum>(String, Boolean, TEnum) |
将一个或多个枚举常量的名称或数值的名称或数值的字符串表示形式转换为等效的枚举对象。 参数指定操作是否区分大小写。 返回值指示转换是否成功。 |
| TryParse<TEnum>(String, TEnum) |
将一个或多个枚举常量的名称或数值的名称或数值的字符串表示形式转换为等效的枚举对象。 返回值指示转换是否成功。 |
TryParse(Type, String, Boolean, Object)
将一个或多个枚举常量的名称或数值的名称或数值的字符串表示形式转换为等效的枚举对象。
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);
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
- result
- Object
此方法返回 true时,包含表示已分析值的枚举常量。
返回
true 如果转换成功,则为 false 否则。
例外
.NET 8 及更高版本:enumType 是布尔支持的枚举类型。
适用于
TryParse(Type, String, Object)
将一个或多个枚举常量的名称或数值的名称或数值的字符串表示形式转换为等效的枚举对象。
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);
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<TEnum>(String, Boolean, TEnum)
将一个或多个枚举常量的名称或数值的名称或数值的字符串表示形式转换为等效的枚举对象。 参数指定操作是否区分大小写。 返回值指示转换是否成功。
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;
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默认值。 此参数未初始化传递。
返回
例外
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>(String, TEnum)
将一个或多个枚举常量的名称或数值的名称或数值的字符串表示形式转换为等效的枚举对象。 返回值指示转换是否成功。
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;
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默认值。 此参数未初始化传递。
返回
例外
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 的成员。