enum的使用
enum num{one,two,there};定义后,我想当输入0时显示one,输入1时显示two,
知道mei ju的元素下标,如何访问显示出元素名?
using System; using System.Reflection; namespace ConsoleApplication7 { class Program { enum num { one=1, two, three }; static void Main(string[] args) { var numT = typeof(num); var input = 0; var myNum = new num(); while ((input=Convert.ToInt32(Console.ReadLine())) != -1) { foreach (var numField in numT.GetFields(BindingFlags.Public | BindingFlags.Static)) { var value= (int)numField.GetValue(myNum); if (value == input) { Console.WriteLine(numField.Name); } } } Console.ReadLine(); } } }