Enum.TryParse 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将一个或多个枚举常量的名称或数值的名称或数值的字符串表示形式转换为等效的枚举对象。 返回值指示转换是否成功。
重载
| 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
- 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
- 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
- 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
- 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
- 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
- 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的默认值。 此参数未初始化传递。
返回
成功转换 value 参数时 true;否则,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
- 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的默认值。 此参数未初始化传递。
返回
成功转换 value 参数时 true;否则,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
- 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 是布尔支持的枚举类型。