Enum.IsDefined 메서드

정의

오버로드

Name Description
IsDefined(Type, Object)

지정된 정수 값 또는 해당 이름을 문자열로 지정한 열거형에 존재하는지 여부를 나타내는 부울을 반환합니다.

IsDefined<TEnum>(TEnum)

지정된 정수 값 또는 해당 이름을 문자열로 지정한 열거형에 존재하는지 여부를 나타내는 부울을 반환합니다.

IsDefined(Type, Object)

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

지정된 정수 값 또는 해당 이름을 문자열로 지정한 열거형에 존재하는지 여부를 나타내는 부울을 반환합니다.

public:
 static bool IsDefined(Type ^ enumType, System::Object ^ value);
public static bool IsDefined(Type enumType, object value);
[System.Runtime.InteropServices.ComVisible(true)]
public static bool IsDefined(Type enumType, object value);
static member IsDefined : Type * obj -> bool
[<System.Runtime.InteropServices.ComVisible(true)>]
static member IsDefined : Type * obj -> bool
Public Shared Function IsDefined (enumType As Type, value As Object) As Boolean

매개 변수

enumType
Type

열거형 형식입니다.

value
Object

에 있는 enumType상수의 값 또는 이름입니다.

반품

상수의 값이 같으면 , 그렇지 않으면 .입니다.

특성

예외

enumType 또는 value .입니다 null.

enumType 가 아닙니다 Enum.

-또는-

형식 value 은 열거형이지만 형식 enumType의 열거형은 아닙니다.

-또는-

형식 value 이 기본 형식 enumType이 아닙니다.

value가 형식SByte, , Int16, Int32, Int64Byte, UInt16UInt32또는 UInt64String또는 .가 아닌 경우

예제

다음 예제에서는 개별 비트 필드로 구성된 명명 PetType 된 열거형을 정의합니다. 그런 다음 여러 비트 필드를 설정하여 발생하는 가능한 기본 열거형 값, 문자열 이름 및 복합 값을 사용하여 메서드를 호출 IsDefined 합니다.

using System;

[Flags] public enum PetType
{
   None = 0, Dog = 1, Cat = 2, Rodent = 4, Bird = 8, Reptile = 16, Other = 32
};

public class Example
{
   public static void Main()
   {
      object value;

      // Call IsDefined with underlying integral value of member.
      value = 1;
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with invalid underlying integral value.
      value = 64;
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with string containing member name.
      value = "Rodent";
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with a variable of type PetType.
      value = PetType.Dog;
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      value = PetType.Dog | PetType.Cat;
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with uppercase member name.
      value = "None";
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      value = "NONE";
      Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with combined value
      value = PetType.Dog | PetType.Bird;
      Console.WriteLine("{0:D}: {1}", value, Enum.IsDefined(typeof(PetType), value));
      value = value.ToString();
      Console.WriteLine("{0:D}: {1}", value, Enum.IsDefined(typeof(PetType), value));
   }
}
// The example displays the following output:
//       1: True
//       64: False
//       Rodent: True
//       Dog: True
//       Dog, Cat: False
//       None: True
//       NONE: False
//       9: False
//       Dog, Bird: False
open System

[<Flags>]
type PetType =
    | None = 0
    | Dog = 1
    | Cat = 2
    | Rodent = 4
    | Bird = 8
    | Reptile = 16
    | Other = 32

[<EntryPoint>]
let main _ =
    // Call IsDefined with underlying integral value of member.
    let value = 1
    printfn $"{value}: {Enum.IsDefined(typeof<PetType>, value)}"
    // Call IsDefined with invalid underlying integral value.
    let value = 64
    printfn $"{value}: {Enum.IsDefined(typeof<PetType>, value)}"
    // Call IsDefined with string containing member name.
    let value = "Rodent"
    printfn $"{value}: {Enum.IsDefined(typeof<PetType>, value)}"
    // Call IsDefined with a variable of type PetType.
    let value = PetType.Dog
    printfn $"{value}: {Enum.IsDefined(typeof<PetType>, value)}"
    let value = PetType.Dog ||| PetType.Cat
    printfn $"{value}: {Enum.IsDefined(typeof<PetType>, value)}"
    // Call IsDefined with uppercase member name.
    let value = "None"
    printfn $"{value}: {Enum.IsDefined(typeof<PetType>, value)}"
    let value = "NONE"
    printfn $"{value}: {Enum.IsDefined(typeof<PetType>, value)}"
    // Call IsDefined with combined value
    let value = PetType.Dog ||| PetType.Bird
    printfn $"{value:D}: {Enum.IsDefined(typeof<PetType>, value)}"
    let value = value.ToString()
    printfn $"{value:D}: {Enum.IsDefined(typeof<PetType>, value)}"
    0
// The example displays the following output:
//       1: True
//       64: False
//       Rodent: True
//       Dog: True
//       Dog, Cat: False
//       None: True
//       NONE: False
//       9: False
//       Dog, Bird: False
<Flags> Public Enum PetType As Integer
   None = 0
   Dog = 1
   Cat = 2
   Rodent = 4
   Bird = 8
   Reptile = 16
   Other = 32
End Enum

Module Example
   Public Sub Main()
      Dim value As Object
      
      ' Call IsDefined with underlying integral value of member.
      value = 1
      Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      ' Call IsDefined with invalid underlying integral value.
      value = 64
      Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      ' Call IsDefined with string containing member name.
      value = "Rodent"
      Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      ' Call IsDefined with a variable of type PetType.
      value = PetType.Dog
      Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      value = PetType.Dog Or PetType.Cat
      Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      ' Call IsDefined with uppercase member name.      
      value = "None"
      Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      value = "NONE"
      Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      ' Call IsDefined with combined value
      value = PetType.Dog Or PetType.Bird
      Console.WriteLine("{0:D}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
      value = value.ToString()
      Console.WriteLine("{0:D}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
   End Sub
End Module
' The example displays the following output:
'       1: True
'       64: False 
'       Rodent: True
'       Dog: True
'       Dog, Cat: False
'       None: True
'       NONE: False
'       9: False
'       Dog, Bird: False

설명

매개 변수는 value 다음 중 어느 것일 수 있습니다.

  • 형식 enumType의 모든 멤버입니다.

  • 값이 형식 enumType의 열거형 멤버인 변수입니다.

  • 열거형 멤버 이름의 문자열 표현입니다. 문자열의 문자는 열거형 멤버 이름과 동일한 대/소문자를 가져야 합니다.

  • 기본 형식의 값입니다 enumType.

상수가 enumType 비트 필드 집합을 정의하고 value 여러 비트 필드의 값, 이름 또는 기본 값을 포함하는 경우 메서드는 IsDefined 반환합니다 false. 즉, 비트 필드 집합을 정의하는 열거형의 경우 메서드는 단일 비트 필드가 열거형에 속하는지 여부만 결정합니다. 특성으로 태그 FlagsAttribute 가 지정된 열거형 형식에서 여러 비트 필드가 설정되었는지 여부를 확인하려면 메서드를 HasFlag 호출할 수 있습니다.

호출자 참고

특성을 사용하여 enumType 정의된 열거형인 경우 FlagsAttribute 메서드는 여러 비트 필드 false 가 설정되어 있지만 value 복합 열거형 값에 해당하지 않는지 또는 여러 비트 플래그 이름의 문자열 연결인지 value 를 반환 value 합니다. 다음 예제에서는 특성으로 Pets 열거형을 FlagsAttribute 정의합니다. 메서드는 IsDefined(Type, Object) 두 비트 필드(falsePets.Dog) 집합이 있는 열거형 값을 전달하고 해당 열거형 값("Dog, Cat")의 문자열 표현을 전달할 때 반환 Pets.Cat 됩니다.

using System;

[Flags] public enum Pets {
      None = 0, Dog = 1, Cat = 2, Bird = 4,
      Rodent = 8, Other = 16 };

public class Example
{
   public static void Main()
   {
      Pets value = Pets.Dog | Pets.Cat;
      Console.WriteLine("{0:D} Exists: {1}",
                        value, Pets.IsDefined(typeof(Pets), value));
      string name = value.ToString();
      Console.WriteLine("{0} Exists: {1}",
                        name, Pets.IsDefined(typeof(Pets), name));
   }
}
// The example displays the following output:
//       3 Exists: False
//       Dog, Cat Exists: False
open System

[<Flags>]
type Pets =
    | None = 0
    | Dog = 1
    | Cat = 2
    | Bird = 4
    | Rodent = 8
    | Other = 16

let value = Pets.Dog ||| Pets.Cat
printfn $"{value:D} Exists: {Pets.IsDefined(typeof<Pets>, value)}"
let name = string value
printfn $"{name} Exists: {Pets.IsDefined(typeof<Pets>, name)}"
// The example displays the following output:
//       3 Exists: False
//       Dog, Cat Exists: False
<Flags> Public Enum Pets As Integer
   None = 0
   Dog = 1
   Cat = 2
   Bird = 4
   Rodent = 8
   Other = 16
End Enum

Module Example
   Public Sub Main()
      Dim value As Pets = Pets.Dog Or Pets.Cat
      Console.WriteLine("{0:D} Exists: {1}", 
                        value, Pets.IsDefined(GetType(Pets), value))
      Dim name As String = value.ToString()
      Console.WriteLine("{0} Exists: {1}", 
                        name, Pets.IsDefined(GetType(Pets), name))
   End Sub
End Module
' The example displays the following output:
'       3 Exists: False
'       Dog, Cat Exists: False

메서드를 호출 HasFlag(Enum) 하여 여러 비트 필드가 설정되었는지 여부를 확인할 수 있습니다.

추가 정보

적용 대상

IsDefined<TEnum>(TEnum)

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

지정된 정수 값 또는 해당 이름을 문자열로 지정한 열거형에 존재하는지 여부를 나타내는 부울을 반환합니다.

public:
generic <typename TEnum>
 where TEnum : value class static bool IsDefined(TEnum value);
public static bool IsDefined<TEnum>(TEnum value) where TEnum : struct;
static member IsDefined : 'Enum -> bool (requires 'Enum : struct)
Public Shared Function IsDefined(Of TEnum As Structure) (value As TEnum) As Boolean

형식 매개 변수

TEnum

열거형의 형식입니다.

매개 변수

value
TEnum

에 있는 TEnum상수의 값 또는 이름입니다.

반품

true 지정된 정수 값 또는 문자열로 이름이 지정된 열거형에 있으면 이고, false 그렇지 않으면.

적용 대상