C# 程序获取指定枚举类型
使用 GetType() 方法来获取枚举类型。
枚举。
Enum[] values = { ConsoleColor.Blue, DayOfWeek.Sunday};现在使用 GetType() 方法获取类型。
Type enumType = val.GetType();
以下是一个显示类型的示例。
示例
using System;
public class Demo {
public static void Main() {
Enum[] values = { ConsoleColor.Blue, DayOfWeek.Sunday};
Console.WriteLine("{0,-5} {1, 10} {2,10}
", "Member", "Enumeration", "UnderlyingType");
foreach (var val in values)
Info(val);
}
static void Info(Enum val) {
Type enumType = val.GetType();
Type underlyingType = Enum.GetUnderlyingType(enumType);
Console.WriteLine("{0, -5} {1, 10} {2,10}", val, enumType.Name, underlyingType.Name);
}
}输出
Member Enumeration UnderlyingType Blue ConsoleColor Int32 Sunday DayOfWeek Int32
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP