ใช้วิธี 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}\n", "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