如何在If语句中评估枚举类型?

3

我第一次尝试在我的代码中使用枚举。我有一个简单的自定义类,看起来像这样:

public class Application
{
    //Properties
    public string AppID { get; set; }
    public string AppName { get; set; }
    public string AppVer { get; set; }
    public enum AppInstallType { msi, exe }
    public string AppInstallArgs { get; set; }
    public string AppInstallerLocation { get; set; }
}

我有一个类里面的方法叫做Install(),代码如下:

    public void Install()
    {
        if (AppInstallType.exe)
        {
            ProcessStartInfo procInfo = new ProcessStartInfo("cmd.exe");
            procInfo.Arguments = "/c msiexec.exe /i " + AppInstallerLocation + " " + AppInstallArgs; ;
            procInfo.WindowStyle = ProcessWindowStyle.Normal;

            Process proc = Process.Start(procInfo);
            proc.WaitForExit();
        }
        else
        {
            ProcessStartInfo procInfo = new ProcessStartInfo("cmd.exe");
            procInfo.Arguments = "/c " + AppInstallerLocation + " " + AppInstallArgs;
            procInfo.WindowStyle = ProcessWindowStyle.Normal;

            Process proc = Process.Start(procInfo);
            proc.WaitForExit();
        }
    }

当AppInstallType是一个字符串时,在我的Install方法开头的If语句工作得很好(AppInstallType =“msi”)。 当我将AppInstallType更改为一个枚举时,我似乎无法弄清楚if语句的语法。

如果可能的话,我希望避免必须向Install()方法传递任何参数。 通过在Application对象上调用Install()方法来安装应用程序将是不错的:

Application app1 = new Application;
app1.AppInstallType = msi;
app1.Install();

我该如何去做?先谢谢了。
1个回答

11

您尚未声明枚举的实例,而只是声明了它。

您需要

    public enum AppInstallType { msi, exe }

public class Application
{
    //Properties
    public string AppID { get; set; }
    public string AppName { get; set; }
    public string AppVer { get; set; }
    public string AppInstallArgs { get; set; }
    public AppInstallType InstallType;
    public string AppInstallerLocation { get; set; }
}

if(InstallType == AppInstallType.msi)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接