Using the Enum.IsDefined()
method. Official Documents
The following is a test case:
Enum class
public enum Roles
{
管理员, 高级用户, 普通用户
}
The main function
static void Main(string[] args)
{
while (true)
{
string role = Console.ReadLine();
bool flag = Enum.IsDefined(typeof(Roles), role);
Console.WriteLine(flag);
}
}
result: