Enum.IsDefined(Type, Object) Método

Definición

Devuelve un valor booleano que indica si existe un valor entero determinado o su nombre como una cadena en una enumeración especificada.

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

Parámetros

enumType
Type

Tipo de enumeración.

value
Object

Valor o nombre de una constante en enumType.

Devoluciones

true es si una constante de enumType tiene un valor igual a value; de lo contrario, falsees .

Atributos

Excepciones

enumType o value es null.

enumTypeno es .Enum

O bien

El tipo de value es una enumeración, pero no es una enumeración de tipo enumType.

O bien

El tipo de value no es un tipo subyacente de enumType.

Ejemplos

En el ejemplo siguiente se define una enumeración denominada PetType que consta de campos de bits individuales. A continuación, llama al IsDefined método con posibles valores de enumeración subyacentes, nombres de cadena y valores compuestos resultantes de establecer varios campos de bits.

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

Comentarios

El value parámetro puede ser cualquiera de los siguientes:

  • Cualquier miembro de tipo enumType.

  • Variable cuyo valor es un miembro de enumeración de tipo enumType.

  • Representación de cadena del nombre de un miembro de enumeración. Los caracteres de la cadena deben tener el mismo caso que el nombre del miembro de enumeración.

  • Valor del tipo subyacente de enumType.

Si las constantes de enumType definen un conjunto de campos de bits y value contienen los valores, nombres o valores subyacentes de varios campos de bits, el IsDefined método devuelve false. En otras palabras, para las enumeraciones que definen un conjunto de campos de bits, el método determina solo si un único campo de bits pertenece a la enumeración. Para determinar si se establecen varios campos de bits en un tipo de enumeración etiquetado con el FlagsAttribute atributo , puede llamar al HasFlag método .

Notas a los autores de las llamadas

Si enumType es una enumeración definida mediante el FlagsAttribute atributo , el método devuelve false si se establecen varios campos de bits, value pero value no se corresponde con un valor de enumeración compuesto, o si value es una concatenación de cadena de los nombres de varias marcas de bits. En el ejemplo siguiente, se define una Pets enumeración con el FlagsAttribute atributo . El IsDefined(Type, Object) método devuelve false cuando se pasa un valor de enumeración que tiene dos campos de bits (Pets.Dog y Pets.Cat) establecidos, y cuando se pasa la representación de cadena de ese valor de enumeración ("Dog, 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

Puede determinar si se establecen varios campos de bits llamando al HasFlag(Enum) método .

Se aplica a

Consulte también