static void Main(string[] args) { Console.WriteLine("使用 EnumConverter "); { EnumConverter converter = new EnumConverter(typeof(ProgrammingLanguage)); // 将字符串转换为枚举. string langStr = "CS, Cpp, XAML"; Console.WriteLine("将字符串 \"{0}\" 转换为枚举...", langStr); ProgrammingLanguage lang = (ProgrammingLanguage)converter.ConvertFromString(langStr); Console.WriteLine("完成!"); // 将枚举转换为字符串. Console.WriteLine("将枚举结果转换为字符串..."); langStr = converter.ConvertToString(lang); Console.WriteLine("完成! \"{0}\"", langStr); } Console.WriteLine("\n使用 EnumDescriptionConverter "); { EnumDescriptionConverter converter = new EnumDescriptionConverter( typeof(ProgrammingLanguage)); // 将枚举转换为字符串. string langStr = "Visual C#, Visual C++, XAML"; Console.WriteLine("将字符串 \"{0}\" 转换为枚举...", langStr); ProgrammingLanguage lang = (ProgrammingLanguage)converter.ConvertFromString(langStr); Console.WriteLine("完成!"); // 将枚举转换为字符串. Console.WriteLine("将枚举结果转换为字符串..."); langStr = converter.ConvertToString(lang); Console.WriteLine("完成! \"{0}\"", langStr); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("Using EnumConverter to"); { EnumConverter converter = new EnumConverter(typeof(ProgrammingLanguage)); // Convert string to enum. string langStr = "CS, Cpp, XAML"; Console.WriteLine("Convert the string \"{0}\" to enum...", langStr); ProgrammingLanguage lang = (ProgrammingLanguage)converter.ConvertFromString(langStr); Console.WriteLine("Done!"); // Convert enum to string. Console.WriteLine("Convert the resulting enum to string..."); langStr = converter.ConvertToString(lang); Console.WriteLine("Done! \"{0}\"", langStr); } Console.WriteLine("\nUsing EnumDescriptionConverter to"); { EnumDescriptionConverter converter = new EnumDescriptionConverter( typeof(ProgrammingLanguage)); // Convert string to enum. string langStr = "Visual C#, Visual C++, XAML"; Console.WriteLine("Convert the string \"{0}\" to enum...", langStr); ProgrammingLanguage lang = (ProgrammingLanguage)converter.ConvertFromString(langStr); Console.WriteLine("Done!"); // Convert enum to string. Console.WriteLine("Convert the resulting enum to string..."); langStr = converter.ConvertToString(lang); Console.WriteLine("Done! \"{0}\"", langStr); } Console.ReadLine(); }