using System; using System.Collections.Generic; using System.Reflection; public static class Terminal { public const int // ... TK_ENTER = 0x28, TK_ESCAPE = 0x29, TK_BACKSPACE = 0x2A; // ... } public static class TerminalHelper { private static Dictionary m_lookup = new Dictionary(); static TerminalHelper() { foreach (FieldInfo f in typeof(Terminal).GetFields()) { m_lookup[(m_lookup[f.Name] = f.GetValue(null).ToString())] = f.Name; } } public static string GetConstName(int code) { return m_lookup[code.ToString()]; // Error handling omitted. } public static int GetConstValue(string name) { return Convert.ToInt32(m_lookup[name]); } } public class Test { public static void Main() { Console.WriteLine("Name of 0x29 is '{0}'", TerminalHelper.GetConstName(Terminal.TK_ESCAPE)); Console.WriteLine("Value of 'TK_ESCAPE' is 0x{0:X}", TerminalHelper.GetConstValue("TK_ESCAPE")); } }