. NET reflection User-Defined Enumeration type,. net reflection-defined Enumeration
Sample Code:
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 ReflectEnumDemo12 {13 //. NET supports symmetric (private key encryption) algorithm types 14 enum Encriptions15 {16 Aes, 17 DES, 18 RC2, 19 Rijndael, 20 TripleDES21} 22 23 public partial class MainForm: form24 {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 (); // obtain all enumerated values 33 34 DataTable dataTable = new DataTable (); // table, to bind 35 dataTable to the UI. columns. addRange (new DataColumn [2] {new DataColumn ("Key", typeof (string), new DataColumn ("Value", typeof (int ))}); // define the table field 36 37 foreach (var item in enumValues) 38 {39 DataRow dataRow = dataTable. newRow (); // defines the row 40 dataRow ["Key"] = type. getEnumName (item); // The column Value is 41 dataRow ["Value"] = (int) item; 42 dataTable. rows. add (dataRow); // insert row 43} 44 45 // cboShow is a ComboxBox control 46 this. cboShow. dataSource = dataTable; // UI data source. The data source must be of the type that implements the IList interface. 47 this. cboShow. valueMember = "Value"; 48 this. cboShow. displayMember = "Key"; 49 }; 50} 51} 52}
Execution result diagram: