C# 如何在If语句中计算Enum?
我第一次尝试在代码中实现Enum。我有一个简单的自定义类,如下所示:C# 如何在If语句中计算Enum?,c#,if-statement,enums,evaluate,C#,If Statement,Enums,Evaluate,我第一次尝试在代码中实现Enum。我有一个简单的自定义类,如下所示: 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
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; }
}
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();
}
}
我在该类中有一个名为Install()的方法,如下所示:
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; }
}
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是字符串时,我的安装方法开头的If语句工作正常(AppInstallType=“msi”)。当我将AppInstallType更改为枚举时,我似乎无法计算if语句的语法
如果可能的话,我希望避免向Install()方法传递任何参数。只需在应用程序对象上调用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)