Enum.GetValues 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 |
|---|---|
| GetValues(Type) |
Recupera una matrice dei valori delle costanti in un'enumerazione specificata. |
| GetValues<TEnum>() |
Recupera una matrice dei valori delle costanti in un tipo di enumerazione specificato. |
GetValues(Type)
- Origine:
- Enum.cs
- Origine:
- Enum.cs
- Origine:
- Enum.cs
- Origine:
- Enum.cs
- Origine:
- Enum.cs
Recupera una matrice dei valori delle costanti in un'enumerazione specificata.
public:
static Array ^ GetValues(Type ^ enumType);
public static Array GetValues(Type enumType);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.")]
public static Array GetValues(Type enumType);
[System.Runtime.InteropServices.ComVisible(true)]
public static Array GetValues(Type enumType);
static member GetValues : Type -> Array
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("It might not be possible to create an array of the enum type at runtime. Use the GetValues<TEnum> overload or the GetValuesAsUnderlyingType method instead.")>]
static member GetValues : Type -> Array
[<System.Runtime.InteropServices.ComVisible(true)>]
static member GetValues : Type -> Array
Public Shared Function GetValues (enumType As Type) As Array
Parametri
- enumType
- Type
Tipo di enumerazione.
Valori restituiti
Matrice che contiene i valori delle costanti in enumType.
- Attributi
Eccezioni
enumType è null.
enumType non è un oggetto Enum.
Il metodo viene richiamato dalla reflection in un contesto di sola reflection,
oppure
enumType è un tipo da un assembly caricato in un contesto di sola reflection.
.NET 8 e versioni successive: enumType è un tipo di enumerazione con supporto booleano.
Esempio
Nell'esempio seguente viene illustrato l'uso di GetValues.
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
Commenti
Gli elementi della matrice sono ordinati in base ai valori binari delle costanti di enumerazione , ovvero in base alla loro grandezza senza segno. Nell'esempio seguente vengono visualizzate informazioni sulla matrice restituita dal GetValues metodo per un'enumerazione che include un valore negativo, zero e un valore positivo.
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
Il GetValues metodo restituisce una matrice che contiene un valore per ogni membro dell'enumerazione enumType . Se più membri hanno lo stesso valore, la matrice restituita include valori duplicati. In questo caso, la chiamata al GetName metodo con ogni valore nella matrice restituita non ripristina i nomi univoci assegnati ai membri con valori duplicati. Per recuperare tutti i nomi dei membri di enumerazione correttamente, chiamare il GetNames metodo .
Il GetValues metodo non può essere richiamato utilizzando la reflection in un contesto di sola reflection. È invece possibile recuperare il valore di tutti i membri dell'enumerazione usando il Type.GetFields metodo per ottenere una matrice di oggetti che rappresentano membri di FieldInfo enumerazione e quindi chiamare il FieldInfo.GetRawConstantValue metodo su ogni elemento della matrice. Nell'esempio seguente viene illustrata questa tecnica. È necessario definire l'enumerazione seguente in un assembly denominato 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
L'assembly viene caricato in un contesto di sola reflection, un Type oggetto che rappresenta l'enumerazione viene creata un'istanza Pets , viene recuperata una matrice di oggetti e i valori dei FieldInfo campi vengono visualizzati nella console.
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
Si applica a
GetValues<TEnum>()
- Origine:
- Enum.cs
- Origine:
- Enum.cs
- Origine:
- Enum.cs
- Origine:
- Enum.cs
- Origine:
- Enum.cs
Recupera una matrice dei valori delle costanti in un tipo di enumerazione specificato.
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()
Parametri di tipo
- TEnum
Tipo dell'enumerazione.
Valori restituiti
Matrice che contiene i valori delle costanti in TEnum.
Eccezioni
.NET 8 e versioni successive: TEnum è un tipo di enumerazione con supporto booleano.