C# 根据枚举检查ReadKey

C# 根据枚举检查ReadKey,c#,if-statement,console,enumeration,C#,If Statement,Console,Enumeration,我有这四个可能的箭头键按下枚举 enum方向{up=ConsoleKey.UpArrow,left=ConsoleKey.LeftArrow,…} private ConsoleKeyInfo用户选择 private bool mQuit; 那么我有 public void getUserInput() { userSelect = Console.ReadKey() if (userSelect.Key == ConsoleKey.Esc

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

enum方向{up=ConsoleKey.UpArrow,left=ConsoleKey.LeftArrow,…}

private ConsoleKeyInfo用户选择

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
       }
    }
无法计算检查“如果按键是枚举中的值之一”的代码
有什么想法吗?

您可以使用cast来解决这个问题

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

}

您可以使用cast来解决此问题

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

}

是不是:
else if(Enum.IsDefined(typeof(Direction),userSelect.Key)){//Logic}
禁止使用注释要求用户接受您的答案。请以后不要这样做。不是吗:
如果(Enum.IsDefined(typeof(Direction),userSelect.Key)){//Logic}
使用注释要求用户接受您的答案是被禁止的。今后请不要这样做。