检查Readum对Enum

时间:2013-03-12 12:01:32

标签: c# if-statement console enumeration

我有四个可能的箭头键按下这个枚举

enum Direction{up=ConsoleKey.UpArrow, left=Consolekey.LeftArrow,...};

private ConsoleKeyInfo userSelect;

private bool mQuit;

然后我

     public void getUserInput()
     {
        userSelect = Console.ReadKey()

        if (userSelect.Key == ConsoleKey.Escape)
        {
            mQuit = true;
        }
       else if(userSelect.Key == "check if key press is value in enumeration")
       {
        //implementation
       }
    }

无法确定代码将检查“如果按键是枚举中的值之一” 有什么想法吗?

2 个答案:

答案 0 :(得分:4)

else if(Enum.IsDefined(typeof(Direction), userSelect.Key)) {
    //Logic
}

答案 1 :(得分:0)

您可以使用强制转换来解决此问题。

if (userSelect.Key == (ConsoleKey)myEnum)
{

}