相关文章推荐
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
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

下列範例會使用 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() );
   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:
Green
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))
        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());
               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
        let colorValue = Enum.Parse(typeof<Colors>, colorString) :?> Colors
        if Enum.IsDefined(typeof<Colors>, colorValue) || (string colorValue).Contains "," then
            printfn $"Converted '{colorString}' to {colorValue}."
            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
            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())
               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
   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.

此作業區分大小寫。

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
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

下列範例會使用 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());
               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
        let colorValue = Enum.Parse(typeof<Colors>, colorString, true) :?> Colors
        if Enum.IsDefined(typeof<Colors>, colorValue) || (string colorValue).Contains "," then
            printfn $"Converted '{colorString}' to {colorValue}."
            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
            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())
               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
   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 參數會指定這項作業是否區分大小寫。

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
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
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
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
 
推荐文章