c# - 3 个条件的三元运算符
问题描述
我在不同的环境(Windows、OsX 和 Linux)中使用 jsReport lib
在Startup.cs
我使用此代码时,启动库
services.AddJsReport(new LocalReporting()
.UseBinary(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? JsReportBinary.GetBinary()
: jsreport.Binary.OSX.JsReportBinary.GetBinary()).AsUtility()
.Create());
因此,如果不是 Windows 平台,他会为 OSX 寻找二进制文件。
但是当有人在 Linux 上使用项目时,他需要将代码更改为:
services.AddJsReport(new LocalReporting()
.UseBinary(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? JsReportBinary.GetBinary()
: jsreport.Binary.Linux.JsReportBinary.GetBinary())
我如何编写使用 Windows 作为主要条件的三元条件,如果没有,它将在 OSX 和 Linux 之间进行选择?
解决方案
services.AddJsReport(new LocalReporting()
.UseBinary(
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? JsReportBinary.GetBinary()
: RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
? Jsreport.Binary.Linux.JsReportBinary.GetBinary()
: Jsreport.Binary.OSX.JsReportBinary.GetBinary())
.Create();
但是只写 3 if
s 并这样做可能会更容易:
// I don't know the exact type, put the correct one here if it isn't this
JsReportBinary binary;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
binary = JsReportBinary.GetBinary();
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
binary = Jsreport.Binary.Linux.JsReportBinary.GetBinary();
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
binary = Jsreport.Binary.OSX.JsReportBinary.GetBinary());
else
binary = null;
services.AddJsReport(new LocalReporting().UseBinary(binary).Create());
推荐阅读
- javascript - 将 iFrame Resizer 与 React JS 页面用作内容
- java - GC 如何在 GC 循环期间停止清理新创建的内存/对象(Java/C#)
- java - 在 Spring Boot 应用程序中使用 @Valid 和 BindingResult 时的表单输入验证问题
- oracle - 除数等于零,需要案例陈述的指导
- npm - 'npm cache clean' 清理了什么?
- python - Pandas 忽略作为参数传递的分隔符
- deezer - Deezer api 选项流式传输持续时间
- react-native - 有没有办法可以使用 Expo React Native 从图像中检测文本?
- r - 使用正确的 purrr::map 函数组合从多级列表创建 df
- c# - 当前不支持“System.String.Split”方法的这种重载