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
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
Public Class GetValuesTest
Enum Colors
Green
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)
Console.WriteLine()
Console.WriteLine("The values of the Styles Enum are:")
For Each i In [Enum].GetValues(GetType(Styles))
Console.WriteLine(i)
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
数组的元素按枚举常量的二进制值 (,即按其无符号) 。 以下示例显示由 方法为包含负值、零和正值的枚举返回
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
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())
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> 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
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())
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
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