我在不同的环境(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作为main的三元条件,如果不是,它将在OSX和Linux之间进行选择?
发布于 2019-10-21 18:50:44
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,并像这样做可能会更容易:
// 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());发布于 2019-10-21 18:52:04
你可以这样做:
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? JsReportBinary.GetBinary() :
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ?
jsreport.Binary.OSX.JsReportBinary.GetBinary() :
jsreport.Binary.Linux.JsReportBinary.GetBinary())发布于 2019-10-21 20:39:30
我还没有测试过,但它会工作的,
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();的好处:我们可以有任意数量的条件。
https://stackoverflow.com/questions/58484698
复制相似问题