using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;public partial class Default2 : System.Web.UI.Page{ enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 }; enum Direction { UP = 1, RIGHT = 2, DOWN = 3, LEFT = 4, }; enum Days { Sat = 0, Sun, Mon, Tue, Wed, Thu, Fri }; protected void Page_Load(object sender, EventArgs e) { ConvertEnum2DataTable( ((Enum) new Colors() ).GetType()); int x = (int)Days.Sun; int y = (int)Days.Fri; Response.Write("Sun = {0}"+ x); Response.Write("Fri = {0}" + y); //由枚舉enum的名稱得到enum的值 Response.Write("<br>int: " + (int)Enum.Parse(typeof(Days), "Thu")); Response.Write("<br>TT: " + (Days)Enum.Parse(typeof(Days), "Thu") ); //根據ID擷取枚舉名稱 1 Response.Write("eee = {0}" + (Days)y); //根據ID擷取枚舉名稱 2 Response.Write("A:" + Enum.GetName(typeof(Days), y)); } private DataTable ConvertEnum2DataTable(Type source) { DataTable dt = new DataTable(); dt.Columns.Add("name"); dt.Columns.Add("value"); foreach (string name in Enum.GetNames(source)) { DataRow dr = dt.Rows.Add(); dr[0] = name; dr[1] =(int) Enum.Parse(source, name); } return dt; } }