Enum.IsDefined Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Overload
| Nome | Descrizione |
|---|---|
| IsDefined(Type, Object) |
Restituisce un valore booleano che indica se un determinato valore integrale o il relativo nome come stringa esiste in un'enumerazione specificata. |
| IsDefined<TEnum>(TEnum) |
Restituisce un valore booleano che indica se un determinato valore integrale o il relativo nome come stringa esiste in un'enumerazione specificata. |
IsDefined(Type, Object)
- Origine:
- Enum.cs
- Origine:
- Enum.cs
- Origine:
- Enum.cs
- Origine:
- Enum.cs
- Origine:
- Enum.cs
Restituisce un valore booleano che indica se un determinato valore integrale o il relativo nome come stringa esiste in un'enumerazione specificata.
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
Parametri
- enumType
- Type
Tipo di enumerazione.
- value
- Object
Valore o nome di una costante in enumType.
Valori restituiti
true se una costante in enumType ha un valore uguale a value; in caso contrario, false.
- Attributi
Eccezioni
enumType o value è null.
enumType non è un oggetto Enum.
oppure
Il tipo di value è un'enumerazione , ma non è un'enumerazione di tipo enumType.
oppure
Il tipo di value non è un tipo sottostante di enumType.
Esempio
Nell'esempio seguente viene definita un'enumerazione denominata PetType costituita da singoli campi di bit. Chiama quindi il IsDefined metodo con possibili valori di enumerazione sottostanti, nomi di stringa e valori compositi risultanti dall'impostazione di più campi di bit.
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
Commenti
Il parametro value può essere uno dei seguenti:
Qualsiasi membro di tipo
enumType.Variabile il cui valore è un membro di enumerazione di tipo
enumType.Rappresentazione di stringa del nome di un membro di enumerazione. I caratteri nella stringa devono avere la stessa distinzione tra maiuscole e minuscole del nome del membro di enumerazione.
Valore del tipo sottostante di
enumType.
Se le costanti in enumType definiscono un set di campi di bit e value contengono i valori, i nomi o i valori sottostanti di più campi di bit, il IsDefined metodo restituisce false. In altre parole, per le enumerazioni che definiscono un set di campi di bit, il metodo determina solo se un campo a bit singolo appartiene all'enumerazione . Per determinare se sono impostati più campi di bit in un tipo di enumerazione contrassegnato con l'attributo FlagsAttribute , è possibile chiamare il HasFlag metodo .
Note per i chiamanti
Se enumType è un'enumerazione definita tramite l'attributo FlagsAttribute , il metodo restituisce false se sono impostati più campi di bit in value ma value non corrispondono a un valore di enumerazione composita o se value è una concatenazione di stringhe dei nomi di più flag di bit. Nell'esempio seguente viene definita un'enumerazione Pets con l'attributo FlagsAttribute . Il IsDefined(Type, Object) metodo restituisce false quando si passa un valore di enumerazione con due campi di bit (Pets.Dog e Pets.Cat) impostati e quando si passa la rappresentazione di stringa del valore di enumerazione ("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
È possibile determinare se sono impostati più campi di bit chiamando il HasFlag(Enum) metodo .
Vedi anche
Si applica a
IsDefined<TEnum>(TEnum)
- Origine:
- Enum.cs
- Origine:
- Enum.cs
- Origine:
- Enum.cs
- Origine:
- Enum.cs
- Origine:
- Enum.cs
Restituisce un valore booleano che indica se un determinato valore integrale o il relativo nome come stringa esiste in un'enumerazione specificata.
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
Parametri di tipo
- TEnum
Tipo dell'enumerazione.
Parametri
- value
- TEnum
Valore o nome di una costante in TEnum.
Valori restituiti
true se un determinato valore integrale, o il relativo nome come stringa, esiste in un'enumerazione specificata; false Altrimenti.