c# enum 枚舉名稱和ID 互換擷取……………..

來源:互聯網
上載者:User
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;        }    }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.