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<string, string> m_lookup = new Dictionary<string, string>();
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"));
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYzsKdXNpbmcgU3lzdGVtLlJlZmxlY3Rpb247CgpwdWJsaWMgc3RhdGljIGNsYXNzIFRlcm1pbmFsCnsKICAgIHB1YmxpYyBjb25zdCBpbnQgCiAgICAvLyAuLi4KICAgIFRLX0VOVEVSICAgICAgICAgICAgPSAweDI4LAogICAgVEtfRVNDQVBFICAgICAgICAgICA9IDB4MjksCiAgICBUS19CQUNLU1BBQ0UgICAgICAgID0gMHgyQTsKICAgIC8vIC4uLgp9CgpwdWJsaWMgc3RhdGljIGNsYXNzIFRlcm1pbmFsSGVscGVyCnsKCXByaXZhdGUgc3RhdGljIERpY3Rpb25hcnk8c3RyaW5nLCBzdHJpbmc+IG1fbG9va3VwID0gbmV3IERpY3Rpb25hcnk8c3RyaW5nLCBzdHJpbmc+KCk7CgkKCXN0YXRpYyBUZXJtaW5hbEhlbHBlcigpCgl7CgkJZm9yZWFjaCAoRmllbGRJbmZvIGYgaW4gdHlwZW9mKFRlcm1pbmFsKS5HZXRGaWVsZHMoKSkKC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