fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4.  
  5. public static class Terminal
  6. {
  7. public const int
  8. // ...
  9. TK_ENTER = 0x28,
  10. TK_ESCAPE = 0x29,
  11. TK_BACKSPACE = 0x2A;
  12. // ...
  13. }
  14.  
  15. public static class TerminalHelper
  16. {
  17. private static Dictionary<string, string> m_lookup = new Dictionary<string, string>();
  18.  
  19. static TerminalHelper()
  20. {
  21. foreach (FieldInfo f in typeof(Terminal).GetFields())
  22. {
  23. m_lookup[(m_lookup[f.Name] = f.GetValue(null).ToString())] = f.Name;
  24. }
  25. }
  26.  
  27. public static string GetConstName(int code)
  28. {
  29. return m_lookup[code.ToString()]; // Error handling omitted.
  30. }
  31.  
  32. public static int GetConstValue(string name)
  33. {
  34. return Convert.ToInt32(m_lookup[name]);
  35. }
  36. }
  37.  
  38. public class Test
  39. {
  40. public static void Main()
  41. {
  42. Console.WriteLine("Name of 0x29 is '{0}'", TerminalHelper.GetConstName(Terminal.TK_ESCAPE));
  43. Console.WriteLine("Value of 'TK_ESCAPE' is 0x{0:X}", TerminalHelper.GetConstValue("TK_ESCAPE"));
  44. }
  45. }
Success #stdin #stdout 0.04s 24256KB
stdin
Standard input is empty
stdout
Name of 0x29 is 'TK_ESCAPE'
Value of 'TK_ESCAPE' is 0x29