示例代码:
复制代码 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace ReflectEnumDemo 12 { 13 //.NET支持的对称(私匙加密)算法类型 14 enum Encriptions 15 { 16 Aes, 17 DES, 18 RC2, 19 Rijndael, 20 TripleDES 21 } 22 23 public partial class MainForm : Form 24 { 25 public MainForm() 26 { 27 InitializeComponent(); 28 29 this.Load += (object sender, EventArgs e) => 30 { 31 Type type = typeof(Encriptions); 32 var enumValues = type.GetEnumValues(); //获取所有枚举值 33 34 DataTable dataTable = new DataTable(); //表,为了与UI绑定 35 dataTable.Columns.AddRange(new DataColumn[2] { new DataColumn("Key", typeof(string)), new DataColumn("Value", typeof(int)) }); //定义表字段 36 37 foreach (var item in enumValues) 38 { 39 DataRow dataRow = dataTable.NewRow(); //定义行 40 dataRow["Key"] = type.GetEnumName(item); //列赋值 41 dataRow["Value"] = (int)item; 42 dataTable.Rows.Add(dataRow); //插入行 43 } 44 45 //cboShow是一个ComboxBox控件 46 this.cboShow.DataSource = dataTable; //UI数据源,数据源必须是实现IList接口的类型 47 this.cboShow.ValueMember = "Value"; 48 this.cboShow.DisplayMember = "Key"; 49 }; 50 } 51 } 52 } 复制代码 执行结果图:
|