我有 ASP.NET 应用程序并想在 if 语句中使用枚举。
我以这种方式获得变量:
string choice = (string)Session["export_choice"];
if(choice == <here goes enum>)
{
}
else
{
}
enum 只能有 2 个字符串值。
我有 ASP.NET 应用程序并想在 if 语句中使用枚举。
我以这种方式获得变量:
string choice = (string)Session["export_choice"];
if(choice == <here goes enum>)
{
}
else
{
}
enum 只能有 2 个字符串值。
这通常是我会使用的地方switch:
myEnumType myEnumChoice;
if (Enum.TryParse(choice, out myEnumChoice))
{
    switch(myEnumChoice)
    {
        case myEnumType.FirstEnum:
            //doSomething();
            break;
        case myEnumType.SecondEnum:
            //doSomethingElse();
            break;
    }
}
else
    throw new ArgumentException(string.Format("Unexpected enum value: {0}", choice));
采用
EnumChoice choice = (EnumChoice) Enum.Parse(typeof(ChumChoice), (string)Session["export_choice"] , true);
EnumChoice 是枚举类型
你可以像这样使用它
if(choice== EnumChoice.X)
{
}
else
{
}