Enum.GetValues 메서드

정의

오버로드

GetValues(Type)

지정된 열거형에서 상수 값의 배열을 검색합니다.

GetValues<TEnum>()

지정된 열거형 형식에서 상수 값의 배열을 검색합니다.

GetValues(Type)

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

지정된 열거형에서 상수 값의 배열을 검색합니다.

public:
 static Array ^ GetValues(Type ^ enumType);
public static Array GetValues (Type enumType);
[System.Runtime.InteropServices.ComVisible(true)]
public static Array GetValues (Type enumType);
static member GetValues : Type -> Array
[<System.Runtime.InteropServices.ComVisible(true)>]
static member GetValues : Type -> Array
Public Shared Function GetValues (enumType As Type) As Array

매개 변수

enumType
Type

열거형 형식입니다.

반환

enumType의 상수 값을 포함하는 배열입니다.

특성

예외

enumType이(가) null인 경우

enumTypeEnum이 아닌 경우

이 메서드는 리플렉션 전용 컨텍스트에서 리플렉션에 의해 호출됩니다.

또는

enumType은 리플렉션 전용 컨텍스트에 로드된 어셈블리의 형식입니다.

.NET 8 이상 버전: enumType 부울 지원 열거형 형식입니다.

예제

다음 예제에서는 GetValues합니다.

using namespace System;
enum class Colors
{
   Red, Green, Blue, Yellow
};

enum class Styles
{
   Plaid = 0,
   Striped = 23,
   Tartan = 65,
   Corduroy = 78
};

int main()
{
   Console::WriteLine(  "The values of the Colors Enum are:" );
   Array^ a = Enum::GetValues( Colors::typeid );
   for ( Int32 i = 0; i < a->Length; i++ )
   {
      Object^ o = a->GetValue( i );
      Console::WriteLine(  "{0}", Enum::Format( Colors::typeid, o,  "D" ) );
   }
   Console::WriteLine();
   Console::WriteLine(  "The values of the Styles Enum are:" );
   Array^ b = Enum::GetValues( Styles::typeid );
   for ( Int32 i = 0; i < b->Length; i++ )
   {
      Object^ o = b->GetValue( i );
      Console::WriteLine(  "{0}", Enum::Format( Styles::typeid, o,  "D" ) );

   }
}
// The example produces the following output:
//       The values of the Colors Enum are:
//       0
//       1
//       2
//       3
//       
//       The values of the Styles Enum are:
//       0
//       23
//       65
//       78
using System;

public class GetValuesTest {
    enum Colors { Red, Green, Blue, Yellow };
    enum Styles { Plaid = 0, Striped = 23, Tartan = 65, Corduroy = 78 };

    public static void Main() {

        Console.WriteLine("The values of the Colors Enum are:");
        foreach(int i in Enum.GetValues(typeof(Colors)))
            Console.WriteLine(i);

        Console.WriteLine();

        Console.WriteLine("The values of the Styles Enum are:");
        foreach(int i in Enum.GetValues(typeof(Styles)))
            Console.WriteLine(i);
    }
}
// The example produces the following output:
//       The values of the Colors Enum are:
//       0
//       1
//       2
//       3
//
//       The values of the Styles Enum are:
//       0
//       23
//       65
//       78
open System

type Colors =
    | Red = 0
    | Green = 1
    | Blue = 2
    | Yellow = 3
    
type Styles =
    | Plaid = 0
    | Striped = 23
    | Tartan = 65
    | Corduroy = 78

printfn $"The values of the Colors Enum are:"
for i in Enum.GetValues typeof<Colors> do
    printfn $"{i}"

printfn "\nThe values of the Styles Enum are:"
for i in Enum.GetValues typeof<Styles> do
    printfn $"{i}"

// The example produces the following output:
//       The values of the Colors Enum are:
//       0
//       1
//       2
//       3
//
//       The values of the Styles Enum are:
//       0
//       23
//       65
//       78
Public Class GetValuesTest
   
    Enum Colors
        Red
        Green
        Blue
        Yellow
    End Enum 'Colors
    
    Enum Styles
        Plaid = 0
        Striped = 23
        Tartan = 65
        Corduroy = 78
    End Enum 'Styles
    
    Public Shared Sub Main()
        
        Console.WriteLine("The values of the Colors Enum are:")
        Dim i As Integer
        For Each i In  [Enum].GetValues(GetType(Colors))
            Console.WriteLine(i)
        Next

        Console.WriteLine()
        
        Console.WriteLine("The values of the Styles Enum are:")
        For Each i In  [Enum].GetValues(GetType(Styles))
            Console.WriteLine(i)
        Next
    End Sub 
End Class 
' The example produces the following output:
'       The values of the Colors Enum are:
'       0
'       1
'       2
'       3
'       
'       The values of the Styles Enum are:
'       0
'       23
'       65
'       78

설명

배열의 요소는 열거형 상수의 이진 값(즉, 부호 없는 크기)을 기준으로 정렬됩니다. 다음 예제에서는 음수 값, 0 및 양수 값을 포함하는 열거형에 대해 메서드에서 반환 GetValues 된 배열에 대한 정보를 표시합니다.

using System;

enum SignMagnitude { Negative = -1, Zero = 0, Positive = 1 };

public class Example
{
   public static void Main()
   {
      foreach (var value in Enum.GetValues(typeof(SignMagnitude))) {
         Console.WriteLine("{0,3}     0x{0:X8}     {1}",
                           (int) value, ((SignMagnitude) value));
}   }
}
// The example displays the following output:
//         0     0x00000000     Zero
//         1     0x00000001     Positive
//        -1     0xFFFFFFFF     Negative
open System

type SignMagnitude =
    | Negative = -1
    | Zero = 0
    | Positive = 1

for value in Enum.GetValues typeof<SignMagnitude> do
    printfn $"{value :?> int,3}     0x{value :?> int:X8}     {value :?> SignMagnitude}"
// The example displays the following output:
//         0     0x00000000     Zero
//         1     0x00000001     Positive
//        -1     0xFFFFFFFF     Negative
Public Enum SignMagnitude As Integer
   Negative = -1 
   Zero = 0
   Positive = 1
End Enum
   
Module Example
   Public Sub Main()
      Dim values() As Integer = CType([Enum].GetValues(GetType(SignMagnitude)), Integer())
      For Each value In values
         Console.WriteLine("{0,3}     0x{0:X8}     {1}",
                           value, CType(value, SignMagnitude).ToString())
      Next
   End Sub
End Module
' The example displays the following output:
'      0     0x00000000     Zero
'      1     0x00000001     Positive
'     -1     0xFFFFFFFF     Negative

메서드는 GetValues 열거형의 각 멤버에 대한 값을 포함하는 배열을 enumType 반환합니다. 여러 멤버의 값이 같으면 반환된 배열에 중복 값이 포함됩니다. 이 경우 반환된 배열의 GetName 각 값을 사용하여 메서드를 호출해도 중복 값이 있는 멤버에 할당된 고유 이름은 복원되지 않습니다. 열거형 멤버의 모든 이름을 성공적으로 검색하려면 메서드를 호출합니다 GetNames .

GetValues 플렉션 전용 컨텍스트에서 리플렉션을 사용하여 메서드를 호출할 수 없습니다. 대신 메서드를 사용하여 Type.GetFields 열거형 멤버를 나타내는 개체의 배열을 가져와서 모든 열거형 멤버의 값을 검색한 다음 배열의 FieldInfo 각 요소에서 메서드를 호출 FieldInfo.GetRawConstantValue 할 수 있습니다. 다음 예제에서는 이 기술을 보여 줍니다. Enumerations.dll 라는 어셈블리에서 다음 열거형을 정의해야 합니다.

[Flags] enum Pets { None=0, Dog=1, Cat=2, Rodent=4, Bird=8,
                    Fish=16, Reptile=32, Other=64 };
[<Flags>] 
type Pets =
    | None = 0
    | Dog = 1
    | Cat = 2
    | Rodent = 4
    | Bird = 8
    | Fish = 16
    | Reptile = 32
    | Other = 64
<Flags> Public Enum Pets As Integer
   None = 0
   Dog = 1
   Cat = 2
   Rodent = 4
   Bird = 8
   Fish = 16
   Reptile = 32
   Other = 64
End Enum

어셈블리가 리플렉션 전용 컨텍스트에 로드되고, Type 열거형을 나타내는 Pets 개체가 인스턴스화되고, 개체 배열 FieldInfo 이 검색되고, 필드 값이 콘솔에 표시됩니다.

using System;
using System.Reflection;

public class Example
{
   public static void Main()
   {
      Assembly assem = Assembly.ReflectionOnlyLoadFrom(@".\Enumerations.dll");
      Type typ = assem.GetType("Pets");
      FieldInfo[] fields = typ.GetFields();

      foreach (var field in fields) {
         if (field.Name.Equals("value__")) continue;

         Console.WriteLine("{0,-9} {1}", field.Name + ":",
                                         field.GetRawConstantValue());
      }
   }
}
// The example displays the following output:
//       None:     0
//       Dog:      1
//       Cat:      2
//       Rodent:   4
//       Bird:     8
//       Fish:     16
//       Reptile:  32
//       Other:    64
open System
open System.Reflection

let assem = Assembly.ReflectionOnlyLoadFrom @".\Enumerations.dll"
let typ = assem.GetType "Pets"
let fields = typ.GetFields()

for field in fields do
    if not (field.Name.Equals "value__") then

        printfn $"""{field.Name + ":",-9} {field.GetRawConstantValue()}"""
// The example displays the following output:
//       None:     0
//       Dog:      1
//       Cat:      2
//       Rodent:   4
//       Bird:     8
//       Fish:     16
//       Reptile:  32
//       Other:    64
Imports System.Reflection

Module Example
   Public Sub Main()
      Dim assem As Assembly = Assembly.ReflectionOnlyLoadFrom(".\Enumerations.dll")
      Dim typ As Type = assem.GetType("Pets")
      Dim fields As FieldInfo() = typ.GetFields

      For Each field In fields
         If field.Name.Equals("value__") Then Continue For
          
         Console.WriteLine("{0,-9} {1}", field.Name + ":", 
                                         field.GetRawConstantValue())
      Next
   End Sub
End Module
' The example displays the following output:
'       None:     0
'       Dog:      1
'       Cat:      2
'       Rodent:   4
'       Bird:     8
'       Fish:     16
'       Reptile:  32
'       Other:    64

적용 대상

GetValues<TEnum>()

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

지정된 열거형 형식에서 상수 값의 배열을 검색합니다.

public:
generic <typename TEnum>
 where TEnum : value class static cli::array <TEnum> ^ GetValues();
public static TEnum[] GetValues<TEnum> () where TEnum : struct;
static member GetValues : unit -> 'Enum[] (requires 'Enum : struct)
Public Shared Function GetValues(Of TEnum As Structure) () As TEnum()

형식 매개 변수

TEnum

열거형의 유형입니다.

반환

TEnum[]

TEnum의 상수 값을 포함하는 배열입니다.

예외

.NET 8 이상 버전: TEnum 부울 지원 열거형 형식입니다.

적용 대상