gpt4 book ai didi

c# - 如何在 If 语句中评估 Enum?

转载 作者:行者123 更新时间:2023-11-30 13:39:06 25 4
gpt4 key购买 nike

我第一次尝试在我的代码中实现枚举。我有一个简单的自定义类,如下所示:

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 更改为 Enum 时,我似乎无法计算出 if 语句的语法。

如果可能的话,我想避免将任何参数传递给 Install() 方法。如果能够仅通过调用 Application 对象上的 Install() 方法来安装应用程序,那就太好了,如下所示:

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

我应该怎么做?提前致谢。

最佳答案

您还没有声明 Enum 的实例,您只是简单地声明了它。

你需要

    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)

关于c# - 如何在 If 语句中评估 Enum?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13693520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com